| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1 | -- |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 2 | -- Copyright (C) 2014-2019 secunet Security Networks AG |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 3 | -- Copyright (C) 2017 Nico Huber <nico.h@gmx.de> |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 4 | -- |
| 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 Huber | 125a29e | 2016-10-18 00:23:54 +0200 | [diff] [blame] | 7 | -- the Free Software Foundation; either version 2 of the License, or |
| 8 | -- (at your option) any later version. |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 9 | -- |
| 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 Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 16 | with HW.MMIO_Range; |
| 17 | pragma Elaborate_All (HW.MMIO_Range); |
| 18 | with HW.PCI.Dev; |
| 19 | pragma Elaborate_All (HW.PCI.Dev); |
| 20 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 21 | with HW.GFX.GMA.Config; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 22 | with HW.GFX.GMA.Config_Helpers; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 23 | with HW.GFX.GMA.Registers; |
| Nico Huber | 312433c | 2019-09-28 03:15:48 +0200 | [diff] [blame] | 24 | with HW.GFX.GMA.PCode; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 25 | with HW.GFX.GMA.Power_And_Clocks; |
| 26 | with HW.GFX.GMA.Panel; |
| 27 | with HW.GFX.GMA.PLLs; |
| 28 | with HW.GFX.GMA.Port_Detect; |
| 29 | with HW.GFX.GMA.Connectors; |
| 30 | with HW.GFX.GMA.Connector_Info; |
| 31 | with HW.GFX.GMA.Pipe_Setup; |
| 32 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 33 | with HW.Debug; |
| 34 | with GNAT.Source_Info; |
| 35 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 36 | |
| 37 | package body HW.GFX.GMA |
| 38 | with Refined_State => |
| 39 | (State => |
| Nico Huber | e317e9c | 2019-09-29 03:03:18 +0200 | [diff] [blame] | 40 | (Config.Variable, |
| 41 | PCI_Usable, |
| Nico Huber | c5c66ec | 2019-09-28 23:59:45 +0200 | [diff] [blame] | 42 | Dev.Address_State, |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 43 | Registers.Address_State, |
| Nico Huber | 312433c | 2019-09-28 03:15:48 +0200 | [diff] [blame] | 44 | PCode.Mailbox_Ready, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 45 | PLLs.State, Panel.Panel_State, |
| Nico Huber | 1a712d3 | 2017-01-09 15:11:04 +0100 | [diff] [blame] | 46 | Cur_Configs, Allocated_PLLs, |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 47 | HPD_Delay, Wait_For_HPD, |
| 48 | Linear_FB_Base), |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 49 | Init_State => Initialized, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 50 | Device_State => |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 51 | (Dev.PCI_State, Registers.Register_State, Registers.GTT_State)) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 52 | is |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 53 | pragma Disable_Atomic_Synchronization; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 54 | |
| Nico Huber | ad09609 | 2024-07-02 18:45:44 +0200 | [diff] [blame] | 55 | subtype Port_Name is String (1 .. 8); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 56 | type Port_Name_Array is array (Port_Type) of Port_Name; |
| 57 | Port_Names : constant Port_Name_Array := |
| Nico Huber | ad09609 | 2024-07-02 18:45:44 +0200 | [diff] [blame] | 58 | (Disabled => "Disabled", |
| 59 | LVDS => "LVDS ", |
| 60 | eDP => "eDP ", |
| 61 | DP1 => "DP1 ", |
| 62 | DP2 => "DP2 ", |
| 63 | DP3 => "DP3 ", |
| 64 | DP_TC1 => "DP_TC1 ", |
| 65 | DP_TC2 => "DP_TC2 ", |
| 66 | DP_TC3 => "DP_TC3 ", |
| 67 | DP_TC4 => "DP_TC4 ", |
| 68 | HDMI1 => "HDMI1 ", |
| 69 | HDMI2 => "HDMI2 ", |
| 70 | HDMI3 => "HDMI3 ", |
| 71 | HDMI_TC1 => "HDMI_TC1", |
| 72 | HDMI_TC2 => "HDMI_TC2", |
| 73 | HDMI_TC3 => "HDMI_TC3", |
| 74 | HDMI_TC4 => "HDMI_TC4", |
| 75 | Analog => "Analog ", |
| 76 | USBC1 => "USBC1 ", |
| 77 | USBC2 => "USBC2 ", |
| 78 | USBC3 => "USBC3 ", |
| 79 | USBC4 => "USBC4 "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 80 | |
| Nico Huber | 4cafa4a | 2020-06-10 16:14:36 +0200 | [diff] [blame^] | 81 | package Dev is new HW.PCI.Dev |
| 82 | (Dev => Config.PCI_Dev, |
| 83 | MMConf_Base => Config.Override_MMConf_Base); |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 84 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 85 | package Display_Controller renames Pipe_Setup; |
| 86 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 87 | type PLLs_Type is array (Pipe_Index) of PLLs.T; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 88 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 89 | type HPD_Type is array (Port_Type) of Boolean; |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 90 | type HPD_Delay_Type is array (Active_Port_Type) of Time.T; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 91 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 92 | Allocated_PLLs : PLLs_Type; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 93 | HPD_Delay : HPD_Delay_Type; |
| 94 | Wait_For_HPD : HPD_Type; |
| 95 | Initialized : Boolean := False; |
| 96 | |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 97 | Linear_FB_Base : Word64; |
| 98 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 99 | ---------------------------------------------------------------------------- |
| 100 | |
| Tim Wawrzynczak | fc49b60 | 2022-09-09 10:29:24 -0600 | [diff] [blame] | 101 | ICP_RAWCLK_NUM : constant := 1 * 2 ** 11; |
| 102 | |
| 103 | function PCH_RAWCLK_FREQ_MASK return Word32 is |
| 104 | Mask : Word32; |
| 105 | begin |
| 106 | if Config.Need_Rawclk_Numerator then |
| 107 | Mask := 16#ffff_ffff#; |
| 108 | elsif Config.Has_Fractional_RawClk then |
| 109 | Mask := 16#3fff# * 2 ** 16; |
| 110 | else |
| 111 | Mask := 16#3ff# * 2 ** 0; |
| 112 | end if; |
| 113 | return Mask; |
| 114 | end PCH_RAWCLK_FREQ_MASK; |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 115 | |
| 116 | function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32 |
| 117 | is |
| 118 | begin |
| Nico Huber | dde0630 | 2020-12-20 02:18:30 +0100 | [diff] [blame] | 119 | if Config.Has_Fractional_RawClk then |
| 120 | declare |
| 121 | Fraction_K : constant Int64 := Freq / 1_000 mod 1_000; |
| 122 | Freq32 : Word32 := Shift_Left (Word32 (Freq / 1_000_000), 16); |
| 123 | begin |
| 124 | if Fraction_K /= 0 then |
| 125 | Freq32 := Freq32 or Shift_Left |
| 126 | (Word32 (Div_Round_Closest (1_000, Fraction_K) - 1), 26); |
| 127 | end if; |
| Tim Wawrzynczak | fc49b60 | 2022-09-09 10:29:24 -0600 | [diff] [blame] | 128 | |
| 129 | if Config.Need_Rawclk_Numerator then |
| 130 | Freq32 := Freq32 or ICP_RAWCLK_NUM; |
| 131 | end if; |
| Nico Huber | dde0630 | 2020-12-20 02:18:30 +0100 | [diff] [blame] | 132 | return Freq32; |
| 133 | end; |
| 134 | else |
| 135 | return Word32 (Freq / 1_000_000); |
| 136 | end if; |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 137 | end PCH_RAWCLK_FREQ; |
| 138 | |
| 139 | ---------------------------------------------------------------------------- |
| 140 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 141 | procedure Enable_Output |
| 142 | (Pipe : in Pipe_Index; |
| 143 | Pipe_Cfg : in Pipe_Config; |
| 144 | Success : out Boolean) |
| Nico Huber | 8a5a3b5 | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 145 | with |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 146 | Pre => |
| 147 | Pipe_Cfg.Port in Active_Port_Type and |
| 148 | Config_Helpers.Valid_FB (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode) |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 149 | is |
| 150 | Port_Cfg : Port_Config; |
| 151 | begin |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 152 | pragma Debug (Debug.New_Line); |
| 153 | pragma Debug (Debug.Put_Line |
| 154 | ("Trying to enable port " & Port_Names (Pipe_Cfg.Port))); |
| 155 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 156 | Config_Helpers.Fill_Port_Config |
| 157 | (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success); |
| 158 | |
| 159 | if Success then |
| Nico Huber | 41e8674 | 2024-07-17 17:10:28 +0200 | [diff] [blame] | 160 | Power_And_Clocks.Power_Up (Pipe_Cfg.Port, Success); |
| 161 | end if; |
| 162 | |
| 163 | if Success then |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 164 | Connector_Info.Preferred_Link_Setting (Port_Cfg, Success); |
| 165 | end if; |
| 166 | |
| Nico Huber | a825448 | 2024-07-03 12:23:00 +0200 | [diff] [blame] | 167 | if Success then |
| 168 | Connectors.Prepare (Pipe_Cfg.Port, Port_Cfg, Success); |
| 169 | end if; |
| 170 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 171 | -- loop over all possible DP-lane configurations |
| 172 | -- (non-DP ports use a single fake configuration) |
| 173 | while Success loop |
| 174 | pragma Loop_Invariant |
| 175 | (Pipe_Cfg.Port in Active_Port_Type and |
| 176 | Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry); |
| 177 | |
| 178 | PLLs.Alloc |
| 179 | (Port_Cfg => Port_Cfg, |
| 180 | PLL => Allocated_PLLs (Pipe), |
| 181 | Success => Success); |
| 182 | |
| 183 | if Success then |
| 184 | -- try each DP-lane configuration twice |
| 185 | for Try in 1 .. 2 loop |
| 186 | pragma Loop_Invariant |
| 187 | (Pipe_Cfg.Port in Active_Port_Type); |
| 188 | |
| Nico Huber | 4798c66 | 2017-01-11 12:44:48 +0100 | [diff] [blame] | 189 | -- Clear pending hot-plug events before every try |
| 190 | Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port); |
| 191 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 192 | Connectors.Pre_On |
| 193 | (Pipe => Pipe, |
| 194 | Port_Cfg => Port_Cfg, |
| 195 | PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)), |
| 196 | Success => Success); |
| 197 | |
| 198 | if Success then |
| 199 | Display_Controller.On |
| 200 | (Pipe => Pipe, |
| Nico Huber | 4d592b4 | 2026-06-23 12:21:37 +0000 | [diff] [blame] | 201 | Pipe_Cfg => Pipe_Cfg, |
| 202 | Port_Cfg => Port_Cfg); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 203 | |
| 204 | Connectors.Post_On |
| Arthur Heymans | 60d0e5f | 2018-03-28 17:08:27 +0200 | [diff] [blame] | 205 | (Pipe => Pipe, |
| 206 | Port_Cfg => Port_Cfg, |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 207 | PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)), |
| 208 | Success => Success); |
| 209 | |
| 210 | if not Success then |
| 211 | Display_Controller.Off (Pipe); |
| Nico Huber | bfea6a3 | 2024-03-07 15:22:36 +0000 | [diff] [blame] | 212 | Connectors.Post_Off (Pipe, Port_Cfg); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 213 | end if; |
| 214 | end if; |
| 215 | |
| 216 | exit when Success; |
| 217 | end loop; |
| 218 | exit when Success; -- connection established => stop loop |
| 219 | |
| 220 | -- connection failed |
| 221 | PLLs.Free (Allocated_PLLs (Pipe)); |
| 222 | end if; |
| 223 | |
| 224 | Connector_Info.Next_Link_Setting (Port_Cfg, Success); |
| Nico Huber | a825448 | 2024-07-03 12:23:00 +0200 | [diff] [blame] | 225 | exit when not Success; |
| 226 | |
| 227 | Connectors.Prepare (Pipe_Cfg.Port, Port_Cfg, Success); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 228 | end loop; |
| 229 | |
| 230 | if Success then |
| 231 | pragma Debug (Debug.Put_Line |
| 232 | ("Enabled port " & Port_Names (Pipe_Cfg.Port))); |
| 233 | else |
| 234 | Wait_For_HPD (Pipe_Cfg.Port) := True; |
| Nico Huber | 2bbd6e7 | 2020-01-07 18:22:59 +0100 | [diff] [blame] | 235 | Panel.Off (Config_Helpers.To_Panel (Pipe_Cfg.Port)); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 236 | end if; |
| 237 | end Enable_Output; |
| 238 | |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 239 | procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config) |
| 240 | is |
| 241 | Port_Cfg : Port_Config; |
| 242 | Success : Boolean; |
| 243 | begin |
| 244 | Config_Helpers.Fill_Port_Config |
| 245 | (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success); |
| 246 | if Success then |
| 247 | pragma Debug (Debug.New_Line); |
| 248 | pragma Debug (Debug.Put_Line |
| 249 | ("Disabling port " & Port_Names (Pipe_Cfg.Port))); |
| 250 | pragma Debug (Debug.New_Line); |
| 251 | |
| Jeremy Compostella | fe80fbb | 2023-01-11 14:05:42 -0700 | [diff] [blame] | 252 | if Pipe_Cfg.Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET then |
| 253 | Display_Controller.Legacy_VGA_Off; |
| 254 | end if; |
| 255 | |
| Nico Huber | bfea6a3 | 2024-03-07 15:22:36 +0000 | [diff] [blame] | 256 | Connectors.Pre_Off (Pipe, Port_Cfg); |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 257 | Display_Controller.Off (Pipe); |
| Nico Huber | bfea6a3 | 2024-03-07 15:22:36 +0000 | [diff] [blame] | 258 | Connectors.Post_Off (Pipe, Port_Cfg); |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 259 | |
| 260 | PLLs.Free (Allocated_PLLs (Pipe)); |
| 261 | end if; |
| 262 | end Disable_Output; |
| 263 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 264 | procedure Update_Outputs (Configs : Pipe_Configs) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 265 | is |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 266 | procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean) |
| 267 | is |
| 268 | HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port)); |
| 269 | begin |
| 270 | if HPD_Delay_Over then |
| 271 | Port_Detect.Hotplug_Detect (Port, Detected); |
| 272 | HPD_Delay (Port) := Time.MS_From_Now (333); |
| 273 | else |
| 274 | Detected := False; |
| 275 | end if; |
| 276 | end Check_HPD; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 277 | |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 278 | Scaler_Reservation : Display_Controller.Scaler_Reservation := |
| 279 | Display_Controller.Null_Scaler_Reservation; |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 280 | |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 281 | Update_Power : Boolean := False; |
| Nico Huber | b0bbdbc | 2019-09-27 22:32:21 +0200 | [diff] [blame] | 282 | Update_CDClk : Boolean; |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 283 | Old_Configs, |
| 284 | New_Configs : Pipe_Configs; |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 285 | |
| 286 | function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean |
| 287 | is |
| 288 | begin |
| 289 | return |
| Nico Huber | 958c564 | 2018-06-02 16:59:31 +0200 | [diff] [blame] | 290 | Cur_Config.Port /= New_Config.Port |
| 291 | or else |
| 292 | Cur_Config.Mode /= New_Config.Mode |
| 293 | or else |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 294 | (Config.Use_PDW_For_EDP_Scaling and then |
| Nico Huber | 8beafd7 | 2020-01-07 14:59:44 +0100 | [diff] [blame] | 295 | (Cur_Config.Port = eDP and |
| Nico Huber | 958c564 | 2018-06-02 16:59:31 +0200 | [diff] [blame] | 296 | Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config))) |
| 297 | or else |
| 298 | (Config.Has_GMCH_PFIT_CONTROL and then |
| 299 | (Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config) or |
| 300 | Scaling_Type (Cur_Config) /= Scaling_Type (New_Config))); |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 301 | end Full_Update; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 302 | begin |
| 303 | Old_Configs := Cur_Configs; |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 304 | New_Configs := Configs; |
| 305 | |
| 306 | -- validate new configs, filter invalid configs and those waiting for HPD |
| 307 | for Pipe in Pipe_Index loop |
| 308 | declare |
| 309 | Success : Boolean := True; |
| 310 | Cur_Config : Pipe_Config renames Cur_Configs (Pipe); |
| 311 | New_Config : Pipe_Config renames New_Configs (Pipe); |
| 312 | begin |
| 313 | if New_Config.Port /= Disabled then |
| 314 | if Wait_For_HPD (New_Config.Port) then |
| 315 | Check_HPD (New_Config.Port, Success); |
| 316 | Wait_For_HPD (New_Config.Port) := not Success; |
| 317 | end if; |
| 318 | |
| 319 | Success := Success and then |
| 320 | Config_Helpers.Validate_Config |
| 321 | (New_Config.Framebuffer, New_Config.Mode, Pipe); |
| 322 | |
| 323 | if Success and then Requires_Scaling (New_Config) then |
| 324 | Display_Controller.Reserve_Scaler |
| 325 | (Success, Scaler_Reservation, Pipe); |
| 326 | end if; |
| 327 | |
| 328 | if not Success then |
| 329 | New_Config.Port := Disabled; |
| 330 | end if; |
| 331 | end if; |
| 332 | end; |
| 333 | pragma Loop_Invariant |
| 334 | (for all P in Pipe_Index'First .. Pipe => |
| 335 | New_Configs (P).Port = Disabled or |
| 336 | Config_Helpers.Valid_FB |
| 337 | (New_Configs (P).Framebuffer, New_Configs (P).Mode)); |
| 338 | end loop; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 339 | |
| Nico Huber | b0bbdbc | 2019-09-27 22:32:21 +0200 | [diff] [blame] | 340 | -- limit dotclocks to maximum CDClk, if we are about |
| 341 | -- to switch CDClk, all pipes have to be disabled |
| 342 | Power_And_Clocks.Limit_Dotclocks (New_Configs, Update_CDClk); |
| 343 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 344 | -- disable all pipes that changed or had a hot-plug event |
| 345 | for Pipe in Pipe_Index loop |
| 346 | declare |
| 347 | Unplug_Detected : Boolean; |
| 348 | Cur_Config : Pipe_Config renames Cur_Configs (Pipe); |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 349 | New_Config : Pipe_Config renames New_Configs (Pipe); |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 350 | begin |
| 351 | if Cur_Config.Port /= Disabled then |
| 352 | Check_HPD (Cur_Config.Port, Unplug_Detected); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 353 | |
| Nico Huber | b0bbdbc | 2019-09-27 22:32:21 +0200 | [diff] [blame] | 354 | if Update_CDClk or |
| 355 | Unplug_Detected or |
| 356 | Full_Update (Cur_Config, New_Config) |
| 357 | then |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 358 | Disable_Output (Pipe, Cur_Config); |
| 359 | Cur_Config.Port := Disabled; |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 360 | Update_Power := True; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 361 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 362 | end if; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 363 | end; |
| 364 | end loop; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 365 | |
| Nico Huber | b0bbdbc | 2019-09-27 22:32:21 +0200 | [diff] [blame] | 366 | -- switch CDClk if necessary and possible, limit dotclocks accordingly |
| 367 | if Update_CDClk then |
| 368 | Power_And_Clocks.Update_CDClk (New_Configs); |
| 369 | end if; |
| 370 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 371 | -- enable all pipes that changed and should be active |
| 372 | for Pipe in Pipe_Index loop |
| 373 | declare |
| 374 | Success : Boolean; |
| 375 | Cur_Config : Pipe_Config renames Cur_Configs (Pipe); |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 376 | New_Config : Pipe_Config renames New_Configs (Pipe); |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 377 | begin |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 378 | -- full update |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 379 | if New_Config.Port /= Disabled and |
| 380 | Full_Update (Cur_Config, New_Config) |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 381 | then |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 382 | Power_And_Clocks.Power_Up (Old_Configs, New_Configs); |
| 383 | Update_Power := True; |
| Nico Huber | c7a4fee | 2016-11-03 18:18:03 +0100 | [diff] [blame] | 384 | |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 385 | Enable_Output (Pipe, New_Config, Success); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 386 | if Success then |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 387 | Cur_Config := New_Config; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 388 | end if; |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 389 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 390 | -- update framebuffer offset only |
| 391 | elsif New_Config.Port /= Disabled and |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 392 | Cur_Config.Framebuffer /= New_Config.Framebuffer |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 393 | then |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 394 | Display_Controller.Setup_FB |
| Nico Huber | 4d592b4 | 2026-06-23 12:21:37 +0000 | [diff] [blame] | 395 | (Pipe, New_Config, New_Config.Mode); |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 396 | Display_Controller.Update_Cursor |
| 397 | (Pipe, New_Config.Framebuffer, New_Config.Cursor); |
| 398 | Cur_Config := New_Config; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 399 | end if; |
| 400 | end; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 401 | end loop; |
| 402 | |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame] | 403 | if Update_Power then |
| 404 | Power_And_Clocks.Power_Down (Old_Configs, New_Configs, Cur_Configs); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 405 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 406 | end Update_Outputs; |
| 407 | |
| 408 | ---------------------------------------------------------------------------- |
| 409 | |
| Nico Huber | 15ffc4f | 2018-01-11 14:44:43 +0100 | [diff] [blame] | 410 | procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type) |
| 411 | is |
| 412 | begin |
| 413 | Cur_Configs (Pipe).Cursor := Cursor; |
| 414 | Display_Controller.Update_Cursor |
| 415 | (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor); |
| 416 | end Update_Cursor; |
| 417 | |
| 418 | procedure Place_Cursor |
| 419 | (Pipe : Pipe_Index; |
| 420 | X : Cursor_Pos; |
| 421 | Y : Cursor_Pos) |
| 422 | is |
| 423 | begin |
| 424 | Cur_Configs (Pipe).Cursor.Center_X := X; |
| 425 | Cur_Configs (Pipe).Cursor.Center_Y := Y; |
| 426 | Display_Controller.Place_Cursor |
| 427 | (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor); |
| 428 | end Place_Cursor; |
| 429 | |
| 430 | procedure Move_Cursor |
| 431 | (Pipe : Pipe_Index; |
| 432 | X : Cursor_Pos; |
| 433 | Y : Cursor_Pos) |
| 434 | is |
| 435 | function Cap_Add (A, B : Cursor_Pos) return Cursor_Pos is |
| 436 | (if A + B < 0 |
| 437 | then Int32'Max (Cursor_Pos'First, A + B) |
| 438 | else Int32'Min (Cursor_Pos'Last, A + B)); |
| 439 | begin |
| 440 | Place_Cursor |
| 441 | (Pipe => Pipe, |
| 442 | X => Cap_Add (Cur_Configs (Pipe).Cursor.Center_X, X), |
| 443 | Y => Cap_Add (Cur_Configs (Pipe).Cursor.Center_Y, Y)); |
| 444 | end Move_Cursor; |
| 445 | |
| 446 | ---------------------------------------------------------------------------- |
| 447 | |
| Nico Huber | bc0588e | 2020-07-21 12:17:19 +0200 | [diff] [blame] | 448 | procedure Backlight_On (Port : Active_Port_Type) |
| 449 | with |
| 450 | Refined_Global => (In_Out => Registers.Register_State) |
| 451 | is |
| 452 | begin |
| 453 | Panel.Backlight_On (Config_Helpers.To_Panel (Port)); |
| 454 | end Backlight_On; |
| 455 | |
| 456 | procedure Backlight_Off (Port : Active_Port_Type) |
| 457 | with |
| 458 | Refined_Global => (In_Out => Registers.Register_State) |
| 459 | is |
| 460 | begin |
| 461 | Panel.Backlight_Off (Config_Helpers.To_Panel (Port)); |
| 462 | end Backlight_Off; |
| 463 | |
| 464 | procedure Set_Brightness (Port : Active_Port_Type; Level : Word32) |
| 465 | with |
| 466 | Refined_Global => (In_Out => Registers.Register_State) |
| 467 | is |
| 468 | begin |
| 469 | Panel.Set_Backlight (Config_Helpers.To_Panel (Port), Level); |
| 470 | end Set_Brightness; |
| 471 | |
| 472 | procedure Get_Max_Brightness (Port : Active_Port_Type; Level : out Word32) |
| 473 | with |
| 474 | Refined_Global => (In_Out => Registers.Register_State) |
| 475 | is |
| 476 | begin |
| 477 | Panel.Get_Max_Backlight (Config_Helpers.To_Panel (Port), Level); |
| 478 | end Get_Max_Brightness; |
| 479 | |
| 480 | ---------------------------------------------------------------------------- |
| 481 | |
| Nico Huber | 793f4f8 | 2022-09-04 14:24:00 +0000 | [diff] [blame] | 482 | pragma Warnings |
| 483 | (GNATprove, Off, """Registers.GTT_State"" * is not modified*", |
| 484 | Reason => "The whole, abstract Device_State is modified in certain configurations."); |
| 485 | pragma Warnings |
| 486 | (GNATprove, Off, "no check message justified*", Reason => "see below"); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 487 | procedure Initialize |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 488 | (Write_Delay : in Word64 := 0; |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 489 | Clean_State : in Boolean := False; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 490 | Success : out Boolean) |
| 491 | with |
| 492 | Refined_Global => |
| Nico Huber | 27088aa | 2018-06-10 13:28:05 +0200 | [diff] [blame] | 493 | (Input => (Time.State), |
| Nico Huber | 793f4f8 | 2022-09-04 14:24:00 +0000 | [diff] [blame] | 494 | In_Out => |
| 495 | (Dev.PCI_State, Port_IO.State, |
| 496 | Registers.Register_State, Registers.GTT_State), |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 497 | Output => |
| Nico Huber | c5c66ec | 2019-09-28 23:59:45 +0200 | [diff] [blame] | 498 | (PCI_Usable, |
| 499 | Config.Variable, |
| Nico Huber | 27088aa | 2018-06-10 13:28:05 +0200 | [diff] [blame] | 500 | Dev.Address_State, |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 501 | Registers.Address_State, |
| Nico Huber | 312433c | 2019-09-28 03:15:48 +0200 | [diff] [blame] | 502 | PCode.Mailbox_Ready, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 503 | PLLs.State, Panel.Panel_State, |
| Nico Huber | 1a712d3 | 2017-01-09 15:11:04 +0100 | [diff] [blame] | 504 | Cur_Configs, Allocated_PLLs, |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 505 | HPD_Delay, Wait_For_HPD, |
| 506 | Linear_FB_Base, Initialized)) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 507 | is |
| 508 | use type HW.Word64; |
| 509 | |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 510 | PCI_MMIO_Base, PCI_GTT_Base : Word64; |
| 511 | |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 512 | function Default_GTT_Base return Word64 is |
| 513 | (Config.Default_MMIO_Base + Word64 (Config.MMIO_GTT_Offset)); |
| 514 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 515 | Now : constant Time.T := Time.Now; |
| 516 | |
| 517 | procedure Check_Platform (Success : out Boolean) |
| 518 | is |
| 519 | Audio_VID_DID : Word32; |
| 520 | begin |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 521 | if Config.Gen_I945 or Config.GMCH_GM965 then |
| 522 | -- i945 and GM965 have no integrated audio DID to verify. |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 523 | Success := True; |
| 524 | return; |
| 525 | end if; |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 526 | Registers.Read_AUD_VID_DID (Audio_VID_DID); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 527 | Success := |
| Nico Huber | 998ee2b | 2018-06-12 23:02:17 +0200 | [diff] [blame] | 528 | ((Config.Gen_Broxton and Audio_VID_DID = 16#8086_280a#) or |
| Nico Huber | 88badbe | 2018-09-27 16:36:47 +0200 | [diff] [blame] | 529 | (Config.CPU_Kabylake and Audio_VID_DID = 16#8086_280b#) or |
| 530 | (Config.CPU_Skylake and Audio_VID_DID = 16#8086_2809#) or |
| Nico Huber | 998ee2b | 2018-06-12 23:02:17 +0200 | [diff] [blame] | 531 | (Config.CPU_Broadwell and Audio_VID_DID = 16#8086_2808#) or |
| 532 | (Config.CPU_Haswell and Audio_VID_DID = 16#8086_2807#) or |
| 533 | ((Config.CPU_Ivybridge or |
| 534 | Config.CPU_Sandybridge) and (Audio_VID_DID = 16#8086_2806# or |
| 535 | Audio_VID_DID = 16#8086_2805#)) or |
| 536 | (Config.CPU_Ironlake and Audio_VID_DID = 16#0000_0000#) or |
| 537 | (Config.Gen_G45 and (Audio_VID_DID = 16#8086_2801# or |
| 538 | Audio_VID_DID = 16#8086_2802# or |
| Tim Wawrzynczak | 605660b | 2022-06-08 12:48:19 -0600 | [diff] [blame] | 539 | Audio_VID_DID = 16#8086_2803#)) or |
| 540 | (Config.CPU_Tigerlake and (Audio_VID_DID = 16#8086_2812#))); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 541 | end Check_Platform; |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 542 | |
| 543 | procedure Check_Platform_PCI (Success : out Boolean) |
| 544 | is |
| 545 | use type HW.Word16; |
| 546 | Vendor, Device : Word16; |
| 547 | begin |
| 548 | Dev.Read16 (Vendor, PCI.Vendor_Id); |
| 549 | Dev.Read16 (Device, PCI.Device_Id); |
| 550 | |
| Nico Huber | 6a996dc | 2018-06-17 16:30:33 +0200 | [diff] [blame] | 551 | Config.Detect_CPU (Device); |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 552 | Success := Vendor = 16#8086# and Config.Compatible_GPU (Device); |
| 553 | end Check_Platform_PCI; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 554 | begin |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 555 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 556 | |
| 557 | pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay)); |
| 558 | |
| Nico Huber | c5c66ec | 2019-09-28 23:59:45 +0200 | [diff] [blame] | 559 | PCI_Usable := False; |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 560 | Linear_FB_Base := 0; |
| Nico Huber | 312433c | 2019-09-28 03:15:48 +0200 | [diff] [blame] | 561 | PCode.Mailbox_Ready := False; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 562 | Wait_For_HPD := HPD_Type'(others => False); |
| 563 | HPD_Delay := HPD_Delay_Type'(others => Now); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 564 | Allocated_PLLs := (others => PLLs.Invalid); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 565 | Cur_Configs := Pipe_Configs' |
| 566 | (others => Pipe_Config' |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 567 | (Port => Disabled, |
| 568 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | 20fd3fc | 2026-06-23 11:26:16 +0000 | [diff] [blame] | 569 | Alignment => Alignment'First, |
| 570 | Scaling => Scaling'First, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 571 | Cursor => Default_Cursor, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 572 | Mode => HW.GFX.Invalid_Mode)); |
| Nico Huber | 27088aa | 2018-06-10 13:28:05 +0200 | [diff] [blame] | 573 | Config.Variable := Config.Initial_Settings; |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 574 | Registers.Set_Register_Base (Config.Default_MMIO_Base, Default_GTT_Base); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 575 | PLLs.Initialize; |
| 576 | |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 577 | Dev.Initialize (Success); |
| 578 | |
| 579 | if Success then |
| Nico Huber | 6a996dc | 2018-06-17 16:30:33 +0200 | [diff] [blame] | 580 | Check_Platform_PCI (Success); |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 581 | if Success then |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 582 | if Config.Has_I945_GTT_BAR then |
| 583 | -- i945: MMIO is on BAR0, GTT is on separate BAR3 |
| 584 | Dev.Map (PCI_MMIO_Base, PCI.Res0); |
| 585 | Dev.Map (PCI_GTT_Base, PCI.Res3); |
| 586 | else |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 587 | Dev.Map |
| 588 | (PCI_MMIO_Base, PCI.Res0, Length => Config.MMIO_GTT_Offset); |
| 589 | Dev.Map |
| 590 | (PCI_GTT_Base, PCI.Res0, Offset => Config.MMIO_GTT_Offset); |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 591 | end if; |
| Nico Huber | 6a996dc | 2018-06-17 16:30:33 +0200 | [diff] [blame] | 592 | if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then |
| 593 | Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base); |
| Nico Huber | c5c66ec | 2019-09-28 23:59:45 +0200 | [diff] [blame] | 594 | PCI_Usable := True; |
| Nico Huber | 6a996dc | 2018-06-17 16:30:33 +0200 | [diff] [blame] | 595 | else |
| 596 | pragma Debug (Debug.Put_Line |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 597 | ("ERROR: Couldn't map resource0.")); |
| Nico Huber | 6a996dc | 2018-06-17 16:30:33 +0200 | [diff] [blame] | 598 | Success := Config.Default_MMIO_Base_Set; |
| 599 | end if; |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 600 | end if; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 601 | else |
| 602 | pragma Debug (Debug.Put_Line |
| 603 | ("WARNING: Couldn't initialize PCI dev.")); |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 604 | Success := Config.Default_MMIO_Base_Set; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 605 | |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 606 | if Success then |
| 607 | Check_Platform (Success); |
| 608 | end if; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 609 | end if; |
| 610 | |
| Nico Huber | 5dbaf4b | 2020-01-08 17:24:58 +0100 | [diff] [blame] | 611 | Panel.Static_Init; -- early for flow analysis |
| 612 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 613 | if not Success then |
| 614 | pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH.")); |
| 615 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 616 | Initialized := False; |
| 617 | return; |
| 618 | end if; |
| 619 | |
| 620 | Panel.Setup_PP_Sequencer; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 621 | |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 622 | if Clean_State then |
| 623 | Power_And_Clocks.Pre_All_Off; |
| 624 | Connectors.Pre_All_Off; |
| 625 | Display_Controller.All_Off; |
| 626 | Connectors.Post_All_Off; |
| 627 | PLLs.All_Off; |
| 628 | Power_And_Clocks.Post_All_Off; |
| Nico Huber | 17d64b6 | 2017-07-15 20:51:25 +0200 | [diff] [blame] | 629 | Registers.Clear_Fences; |
| Nico Huber | 33912aa | 2016-12-06 20:36:23 +0100 | [diff] [blame] | 630 | else |
| 631 | -- According to PRMs, VGA plane is the only thing |
| Nico Huber | 3a0e2a0 | 2017-07-19 14:41:46 +0200 | [diff] [blame] | 632 | -- that's enabled by default after reset... |
| Nico Huber | 33912aa | 2016-12-06 20:36:23 +0100 | [diff] [blame] | 633 | Display_Controller.Legacy_VGA_Off; |
| Nico Huber | 3a0e2a0 | 2017-07-19 14:41:46 +0200 | [diff] [blame] | 634 | -- ... along with some DDI port bits since Skylake. |
| 635 | Connectors.Post_Reset_Off; |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 636 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 637 | |
| 638 | -------------------- Now restart from a clean state --------------------- |
| 639 | Power_And_Clocks.Initialize; |
| Nico Huber | 1b99185 | 2024-07-17 16:44:05 +0200 | [diff] [blame] | 640 | Port_Detect.Initialize; |
| 641 | Connectors.Initialize; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 642 | |
| Nico Huber | 1c3b928 | 2017-02-09 13:57:04 +0100 | [diff] [blame] | 643 | if Config.Has_PCH then |
| 644 | Registers.Unset_And_Set_Mask |
| 645 | (Register => Registers.PCH_RAWCLK_FREQ, |
| 646 | Mask_Unset => PCH_RAWCLK_FREQ_MASK, |
| Nico Huber | c9ad9de | 2020-12-20 02:34:37 +0100 | [diff] [blame] | 647 | Mask_Set => PCH_RAWCLK_FREQ (Config.Raw_Clock)); |
| Nico Huber | 1c3b928 | 2017-02-09 13:57:04 +0100 | [diff] [blame] | 648 | end if; |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 649 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 650 | Initialized := True; |
| 651 | |
| 652 | end Initialize; |
| Nico Huber | 793f4f8 | 2022-09-04 14:24:00 +0000 | [diff] [blame] | 653 | pragma Annotate |
| 654 | (GNATprove, Intentional, "unused global", |
| 655 | "The whole, abstract Device_State is modified in certain configurations."); |
| 656 | pragma Warnings (GNATprove, On, "no check message justified*"); |
| 657 | pragma Warnings |
| 658 | (GNATprove, On, """Registers.GTT_State"" * is not modified*"); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 659 | |
| 660 | function Is_Initialized return Boolean |
| 661 | with |
| 662 | Refined_Post => Is_Initialized'Result = Initialized |
| 663 | is |
| 664 | begin |
| 665 | return Initialized; |
| 666 | end Is_Initialized; |
| 667 | |
| 668 | ---------------------------------------------------------------------------- |
| 669 | |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 670 | pragma Warnings |
| 671 | (GNATprove, Off, """Registers.Register_State"" * is not modified*", |
| Nico Huber | adfe11f | 2018-06-10 14:59:04 +0200 | [diff] [blame] | 672 | Reason => "Power_Up_VGA is only effective in certain configurations."); |
| Nico Huber | 17b513e | 2022-09-04 13:36:02 +0200 | [diff] [blame] | 673 | pragma Warnings |
| Tim Wawrzynczak | 5473d29 | 2023-02-06 16:46:33 -0700 | [diff] [blame] | 674 | (GNATprove, Off, """PCode.Mailbox_Ready"" * is not modified*", |
| 675 | Reason => "Pcode is only used in certain configurations."); |
| 676 | pragma Warnings |
| Nico Huber | 17b513e | 2022-09-04 13:36:02 +0200 | [diff] [blame] | 677 | (GNATprove, Off, "no check message justified*", Reason => "see below"); |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 678 | procedure Power_Up_VGA |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 679 | with |
| 680 | Refined_Global => |
| Nico Huber | adfe11f | 2018-06-10 14:59:04 +0200 | [diff] [blame] | 681 | (Input => (Cur_Configs, Config.Variable, Time.State), |
| Tim Wawrzynczak | 5473d29 | 2023-02-06 16:46:33 -0700 | [diff] [blame] | 682 | In_Out => (Registers.Register_State, PCode.Mailbox_Ready), |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 683 | Proof_In => (Initialized)) |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 684 | is |
| 685 | Fake_Config : constant Pipe_Configs := |
| 686 | (Primary => |
| 687 | (Port => Analog, |
| 688 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | 20fd3fc | 2026-06-23 11:26:16 +0000 | [diff] [blame] | 689 | Alignment => Center, |
| 690 | Scaling => Fit, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 691 | Cursor => Default_Cursor, |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 692 | Mode => HW.GFX.Invalid_Mode), |
| 693 | others => |
| 694 | (Port => Disabled, |
| 695 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | 20fd3fc | 2026-06-23 11:26:16 +0000 | [diff] [blame] | 696 | Alignment => Alignment'First, |
| 697 | Scaling => Scaling'First, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 698 | Cursor => Default_Cursor, |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 699 | Mode => HW.GFX.Invalid_Mode)); |
| 700 | begin |
| 701 | Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config); |
| 702 | end Power_Up_VGA; |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 703 | pragma Annotate |
| 704 | (GNATprove, Intentional, "unused global", |
| Nico Huber | adfe11f | 2018-06-10 14:59:04 +0200 | [diff] [blame] | 705 | "Power_Up_VGA is only effective in certain configurations."); |
| Tim Wawrzynczak | 5473d29 | 2023-02-06 16:46:33 -0700 | [diff] [blame] | 706 | pragma Warnings |
| 707 | (GNATprove, On, """PCode.Mailbox_Ready"" * is not modified*"); |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 708 | pragma Warnings (GNATprove, On, "no check message justified*"); |
| 709 | pragma Warnings |
| 710 | (GNATprove, On, """Registers.Register_State"" * is not modified*"); |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 711 | |
| 712 | ---------------------------------------------------------------------------- |
| 713 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 714 | function FB_First_Page (FB : Framebuffer_Type) return Natural is |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 715 | (Natural (Phys_Offset (FB) / GTT_Page_Size)); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 716 | function FB_Pages (FB : Framebuffer_Type) return Natural is |
| 717 | (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size))); |
| 718 | function FB_Last_Page (FB : Framebuffer_Type) return Natural is |
| 719 | (FB_First_Page (FB) + FB_Pages (FB) - 1); |
| 720 | |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 721 | -- Check basics and that it fits in GTT. For 90 degree rotations, |
| 722 | -- the Offset should be above GTT_Rotation_Offset. The latter will |
| 723 | -- be subtracted for the aperture mapping. |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 724 | function Valid_FB (FB : Framebuffer_Type) return Boolean is |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 725 | (Valid_Stride (FB) and |
| 726 | FB_First_Page (FB) in GTT_Range and |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 727 | FB_Last_Page (FB) + 128 in GTT_Range and |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 728 | (not Rotation_90 (FB) or |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 729 | (FB_First_Page (FB) mod 64 = 0 and |
| 730 | FB_Last_Page (FB) + 128 + GTT_Rotation_Offset in GTT_Range and |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 731 | FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size))); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 732 | |
| 733 | -- Also check that we don't overflow the GTT's 39-bit space |
| 734 | -- (always true with a 32-bit base) |
| 735 | function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32) |
| 736 | return Boolean is |
| 737 | (Valid_FB (FB) and |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 738 | Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <= |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 739 | Int64 (GTT_Address_Type'Last)) |
| 740 | with |
| 741 | Ghost; |
| 742 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 743 | procedure Write_GTT |
| 744 | (GTT_Page : GTT_Range; |
| 745 | Device_Address : GTT_Address_Type; |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 746 | Valid : Boolean) |
| 747 | is |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 748 | begin |
| 749 | Registers.Write_GTT (GTT_Page, Device_Address, Valid); |
| 750 | end Write_GTT; |
| 751 | |
| Nico Huber | ceda17d | 2018-06-09 22:00:29 +0200 | [diff] [blame] | 752 | procedure Read_GTT |
| 753 | (Device_Address : out GTT_Address_Type; |
| 754 | Valid : out Boolean; |
| 755 | GTT_Page : in GTT_Range) |
| 756 | is |
| 757 | begin |
| 758 | Registers.Read_GTT (Device_Address, Valid, GTT_Page); |
| 759 | end Read_GTT; |
| 760 | |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 761 | procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32) |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 762 | with |
| 763 | Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 764 | is |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 765 | Phys_Addr : GTT_Address_Type := |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 766 | GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB)); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 767 | begin |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 768 | for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 769 | Registers.Write_GTT |
| 770 | (GTT_Page => Idx, |
| 771 | Device_Address => Phys_Addr, |
| 772 | Valid => True); |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 773 | Phys_Addr := Phys_Addr + GTT_Page_Size; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 774 | end loop; |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 775 | -- Add another 128 dummy pages to work around buggy VT-d |
| 776 | for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop |
| 777 | Registers.Write_GTT (Idx, Phys_Addr, True); |
| 778 | end loop; |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 779 | |
| 780 | if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then |
| 781 | declare |
| 782 | V_Pages : constant Natural := Natural (FB.V_Stride) / 32; |
| 783 | Bytes_Per_Row : constant GTT_Address_Type := |
| 784 | GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB)); |
| 785 | begin |
| 786 | Phys_Addr := GTT_Address_Type (Phys_Base) + |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 787 | GTT_Address_Type (Phys_Offset (FB)) + |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 788 | GTT_Address_Type (FB_Size (FB)); |
| 789 | for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop |
| 790 | Phys_Addr := Phys_Addr - Bytes_Per_Row; |
| 791 | Registers.Write_GTT |
| 792 | (GTT_Page => GTT_Rotation_Offset + Page, |
| 793 | Device_Address => Phys_Addr, |
| 794 | Valid => True); |
| 795 | |
| 796 | if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then |
| 797 | Phys_Addr := Phys_Addr + GTT_Page_Size + |
| 798 | GTT_Address_Type (V_Pages) * Bytes_Per_Row; |
| 799 | end if; |
| 800 | end loop; |
| 801 | end; |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 802 | -- Add another 128 dummy pages to work around buggy VT-d |
| 803 | for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop |
| 804 | Registers.Write_GTT (GTT_Rotation_Offset + Idx, Phys_Addr, True); |
| 805 | end loop; |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 806 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 807 | end Setup_Default_GTT; |
| 808 | |
| 809 | ---------------------------------------------------------------------------- |
| 810 | |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 811 | use type HW.Word16; |
| 812 | subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33; |
| 813 | |
| 814 | function GGMS_Gen4 (GGC : Word16) return Natural is |
| 815 | (Natural (Shift_Right (GGC, 8) and 16#07#)); |
| 816 | function GTT_Size_Gen4 (GGC : Word16) return Natural is |
| 817 | (if GGMS_Gen4 (GGC) in 1 .. 3 then |
| 818 | (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0); |
| 819 | |
| 820 | function GMS_Gen4 (GGC : Word16) return Natural is |
| 821 | (Natural (Shift_Right (GGC, 4) and 16#0f#)); |
| 822 | Valid_Stolen_Size_Gen4 : constant |
| 823 | array (Natural range 1 .. 13) of Stolen_Size_Range := |
| 824 | (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352); |
| 825 | function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is |
| 826 | (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then |
| Arthur Heymans | 5fd9a31 | 2017-09-12 12:45:18 +0200 | [diff] [blame] | 827 | Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0); |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 828 | |
| 829 | function GTT_Size_Gen6 (GGC : Word16) return Natural is |
| 830 | (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20); |
| 831 | |
| 832 | function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is |
| 833 | (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20); |
| 834 | |
| Nico Huber | fe7985f | 2019-10-12 22:19:24 +0200 | [diff] [blame] | 835 | function GGMS_Gen8 (GGC : Word16) return Natural is |
| 836 | (Natural (Shift_Right (GGC, 6) and 16#03#)); |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 837 | function GTT_Size_Gen8 (GGC : Word16) return Natural is |
| Nico Huber | fe7985f | 2019-10-12 22:19:24 +0200 | [diff] [blame] | 838 | (if GGMS_Gen8 (GGC) /= 0 then |
| 839 | Natural (Shift_Left (Word32'(1), 20 + GGMS_Gen8 (GGC))) else 0); |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 840 | |
| 841 | function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is |
| 842 | (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#)); |
| 843 | function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is |
| 844 | (GMS_Gen8 (GGC) * 32 * 2 ** 20); |
| 845 | |
| 846 | function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is |
| 847 | (if GMS_Gen8 (GGC) < 16#f0# then |
| 848 | Stolen_Size_Gen8 (GGC) |
| 849 | else |
| 850 | (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20); |
| 851 | |
| 852 | procedure Decode_Stolen |
| 853 | (GTT_Size : out Natural; |
| 854 | Stolen_Size : out Stolen_Size_Range) |
| 855 | with |
| 856 | Pre => Is_Initialized |
| 857 | is |
| Nico Huber | 63ec836 | 2018-06-09 17:42:19 +0200 | [diff] [blame] | 858 | GGC_Reg : constant PCI.Index := |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 859 | (if Config.Gen_I945 or Config.Gen_G45 or Config.CPU_Ironlake |
| 860 | then 16#52# else 16#50#); |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 861 | GGC : Word16; |
| 862 | begin |
| 863 | Dev.Read16 (GGC, GGC_Reg); |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 864 | if Config.Gen_I945 then |
| 865 | -- i945 GTT is on a separate BAR3; GGC GGMS encoding differs |
| 866 | -- from Gen4+. Match the Linux driver and use the BAR size. |
| 867 | Dev.Resource_Size (GTT_Size, PCI.Res3); |
| 868 | Stolen_Size := Stolen_Size_Gen4 (GGC); |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 869 | elsif Config.GMCH_GM965 then |
| 870 | -- GM965 has no GGMS field in GGC. The GTT is a fixed 512 KiB |
| 871 | -- region in the upper half of the 1 MiB GTTMMADR BAR. |
| 872 | GTT_Size := 512 * 2 ** 10; |
| 873 | Stolen_Size := Stolen_Size_Gen4 (GGC); |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 874 | elsif Config.Gen_G45 or Config.CPU_Ironlake then |
| Nico Huber | 998ee2b | 2018-06-12 23:02:17 +0200 | [diff] [blame] | 875 | GTT_Size := GTT_Size_Gen4 (GGC); |
| 876 | Stolen_Size := Stolen_Size_Gen4 (GGC); |
| 877 | elsif Config.CPU_Sandybridge or Config.CPU_Ivybridge or Config.CPU_Haswell |
| 878 | then |
| 879 | GTT_Size := GTT_Size_Gen6 (GGC); |
| 880 | Stolen_Size := Stolen_Size_Gen6 (GGC); |
| 881 | elsif Config.CPU_Broadwell then |
| 882 | GTT_Size := GTT_Size_Gen8 (GGC); |
| 883 | Stolen_Size := Stolen_Size_Gen8 (GGC); |
| 884 | else |
| 885 | GTT_Size := GTT_Size_Gen8 (GGC); |
| 886 | Stolen_Size := Stolen_Size_Gen9 (GGC); |
| 887 | end if; |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 888 | end Decode_Stolen; |
| 889 | |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 890 | procedure GTT_Entry_Count (Count : out Natural) |
| 891 | is |
| 892 | GTT_Size : Natural; |
| 893 | |
| 894 | procedure Fake_Config_State_Access |
| 895 | with |
| 896 | Global => (Input => Config.Variable), |
| 897 | Annotate => (GNATprove, Intentional, "unused global", |
| 898 | "Used to have a common contract across platforms."); |
| 899 | procedure Fake_Config_State_Access is null; |
| 900 | begin |
| 901 | Fake_Config_State_Access; |
| 902 | |
| 903 | if Config.Has_I945_GTT_BAR then |
| 904 | -- i945 GTT is on a separate BAR3; its size is the BAR size. |
| 905 | Dev.Resource_Size (GTT_Size, PCI.Res3); |
| Arthur Heymans | 3f37cce | 2026-03-03 18:52:12 +0100 | [diff] [blame] | 906 | elsif Config.GMCH_GM965 then |
| 907 | -- GM965 has no GGMS field in GGC; its GTT is fixed at 512 KiB. |
| 908 | GTT_Size := 512 * 2 ** 10; |
| Arthur Heymans | 960e239 | 2026-03-03 19:45:24 +0100 | [diff] [blame] | 909 | else |
| 910 | -- Gen4+: GTT size is encoded in the GGC register. |
| 911 | declare |
| 912 | GGC_Reg : constant PCI.Index := |
| 913 | (if Config.Gen_G45 or Config.CPU_Ironlake |
| 914 | then 16#52# else 16#50#); |
| 915 | GGC : Word16; |
| 916 | begin |
| 917 | Dev.Read16 (GGC, GGC_Reg); |
| 918 | if Config.Gen_G45 or Config.CPU_Ironlake then |
| 919 | GTT_Size := GTT_Size_Gen4 (GGC); |
| 920 | elsif Config.CPU_Sandybridge or |
| 921 | Config.CPU_Ivybridge or |
| 922 | Config.CPU_Haswell |
| 923 | then |
| 924 | GTT_Size := GTT_Size_Gen6 (GGC); |
| 925 | else |
| 926 | GTT_Size := GTT_Size_Gen8 (GGC); |
| 927 | end if; |
| 928 | end; |
| 929 | end if; |
| 930 | Count := Natural'Min (GTT_Size / Config.GTT_PTE_Size, GTT_Range'Last + 1); |
| 931 | end GTT_Entry_Count; |
| 932 | |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 933 | -- Additional runtime validation that FB fits stolen memory and aperture. |
| 934 | procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean) |
| 935 | with |
| 936 | Pre => Is_Initialized, |
| 937 | Post => (if Valid then Valid_FB (FB)) |
| 938 | is |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 939 | GTT_Off : constant Natural := |
| 940 | (if Rotation_90 (FB) then GTT_Rotation_Offset else 0); |
| 941 | |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 942 | GTT_Size, Aperture_Size : Natural; |
| 943 | Stolen_Size : Stolen_Size_Range; |
| 944 | begin |
| 945 | Valid := Valid_FB (FB); |
| 946 | |
| 947 | if Valid then |
| 948 | Decode_Stolen (GTT_Size, Stolen_Size); |
| 949 | Dev.Resource_Size (Aperture_Size, PCI.Res2); |
| 950 | Valid := |
| Nico Huber | 2e87c0d | 2020-04-18 00:46:39 +0200 | [diff] [blame] | 951 | FB_Last_Page (FB) + 128 + GTT_Off < GTT_Size / Config.GTT_PTE_Size |
| 952 | and |
| 953 | FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size) |
| 954 | and |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 955 | FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size; |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 956 | pragma Debug (not Valid, Debug.Put_Line |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 957 | ("Stolen memory too small to hold framebuffer.")); |
| 958 | end if; |
| 959 | end Validate_FB; |
| 960 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 961 | procedure Setup_Default_FB |
| 962 | (FB : in Framebuffer_Type; |
| 963 | Clear : in Boolean := True; |
| 964 | Success : out Boolean) |
| 965 | is |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 966 | GMA_Phys_Base_Mask : constant := 16#fff0_0000#; |
| 967 | |
| 968 | Phys_Base : Word32; |
| 969 | begin |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 970 | Validate_FB (FB, Success); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 971 | |
| 972 | if Success then |
| Tim Wawrzynczak | 1b65b84 | 2022-09-09 10:23:06 -0600 | [diff] [blame] | 973 | if Config.GMA_Base_Is_64bit then |
| 974 | Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index + 4); |
| 975 | if Phys_Base /= 0 then |
| 976 | pragma Debug (Debug.Put_Line ("Cannot handle 64-bit DSM yet.")); |
| 977 | Success := False; |
| 978 | return; |
| 979 | end if; |
| 980 | end if; |
| 981 | |
| 982 | Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 983 | Phys_Base := Phys_Base and GMA_Phys_Base_Mask; |
| 984 | Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0; |
| 985 | pragma Debug (not Success, Debug.Put_Line |
| 986 | ("Failed to read stolen memory base.")); |
| Nico Huber | 0164b02 | 2017-08-24 15:12:51 +0200 | [diff] [blame] | 987 | |
| 988 | if Success then |
| 989 | if FB.Tiling in XY_Tiling then |
| 990 | Registers.Add_Fence |
| 991 | (First_Page => FB_First_Page (FB), |
| 992 | Last_Page => FB_Last_Page (FB), |
| 993 | Tiling => FB.Tiling, |
| 994 | Pitch => FB_Pitch (FB.Stride, FB), |
| 995 | Success => Success); |
| 996 | end if; |
| 997 | pragma Debug (not Success, Debug.Put_Line |
| 998 | ("Tiled framebuffer but no fence regs available.")); |
| 999 | end if; |
| 1000 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 1001 | if Success then |
| 1002 | Setup_Default_GTT (FB, Phys_Base); |
| 1003 | end if; |
| 1004 | end if; |
| 1005 | |
| 1006 | if Success and then Clear then |
| 1007 | declare |
| 1008 | use type HW.Word64; |
| 1009 | Linear_FB : Word64; |
| 1010 | begin |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 1011 | Map_Linear_FB (Linear_FB, FB); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 1012 | if Linear_FB /= 0 then |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 1013 | Framebuffer_Filler.Fill (Linear_FB, FB); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 1014 | end if; |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 1015 | end; |
| 1016 | end if; |
| 1017 | end Setup_Default_FB; |
| 1018 | |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 1019 | procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type) |
| 1020 | is |
| 1021 | use type HW.Word64; |
| 1022 | |
| 1023 | Valid : Boolean; |
| 1024 | begin |
| 1025 | Linear_FB := 0; |
| 1026 | |
| 1027 | if Linear_FB_Base = 0 then |
| 1028 | Dev.Map (Linear_FB_Base, PCI.Res2); |
| 1029 | pragma Debug |
| 1030 | (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2.")); |
| 1031 | end if; |
| 1032 | |
| 1033 | if Linear_FB_Base /= 0 then |
| 1034 | Validate_FB (FB, Valid); |
| 1035 | if Valid then |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 1036 | Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB)); |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 1037 | end if; |
| 1038 | end if; |
| 1039 | end Map_Linear_FB; |
| 1040 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 1041 | ---------------------------------------------------------------------------- |
| 1042 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 1043 | procedure Dump_Configs (Configs : Pipe_Configs) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1044 | is |
| 1045 | subtype Pipe_Name is String (1 .. 9); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 1046 | type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1047 | Pipe_Names : constant Pipe_Name_Array := |
| 1048 | (Primary => "Primary ", |
| 1049 | Secondary => "Secondary", |
| 1050 | Tertiary => "Tertiary "); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1051 | |
| 1052 | subtype Tiling_Name is String (1 .. 7); |
| 1053 | type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name; |
| 1054 | Tilings : constant Tiling_Name_Array := |
| 1055 | (Linear => "Linear ", |
| 1056 | X_Tiled => "X_Tiled", |
| 1057 | Y_Tiled => "Y_Tiled"); |
| 1058 | |
| 1059 | subtype Rotation_Name is String (1 .. 11); |
| 1060 | type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name; |
| 1061 | Rotations : constant Rotation_Name_Array := |
| 1062 | (No_Rotation => "No_Rotation", |
| 1063 | Rotated_90 => "Rotated_90 ", |
| 1064 | Rotated_180 => "Rotated_180", |
| 1065 | Rotated_270 => "Rotated_270"); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1066 | begin |
| 1067 | Debug.New_Line; |
| Paul Menzel | b83107c | 2017-05-04 09:02:33 +0200 | [diff] [blame] | 1068 | Debug.Put_Line ("CONFIG =>"); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 1069 | for Pipe in Pipe_Index loop |
| 1070 | if Pipe = Pipe_Index'First then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1071 | Debug.Put (" ("); |
| 1072 | else |
| 1073 | Debug.Put (" "); |
| 1074 | end if; |
| Nico Huber | fb80da9 | 2026-07-02 15:01:13 +0200 | [diff] [blame] | 1075 | Debug.Put (Pipe_Names (Pipe)); |
| 1076 | Debug.Put_Line (" =>"); |
| 1077 | Debug.Put (" (Port => "); |
| 1078 | Debug.Put (Port_Names (Configs (Pipe).Port)); |
| 1079 | Debug.Put_Line (","); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1080 | Debug.Put_Line (" Framebuffer =>"); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1081 | Debug.Put (" (Width => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1082 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width); |
| 1083 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1084 | Debug.Put (" Height => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1085 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height); |
| 1086 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1087 | Debug.Put (" Start_X => "); |
| 1088 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X); |
| 1089 | Debug.Put_Line (","); |
| 1090 | Debug.Put (" Start_Y => "); |
| 1091 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y); |
| 1092 | Debug.Put_Line (","); |
| 1093 | Debug.Put (" Stride => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1094 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride); |
| 1095 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1096 | Debug.Put (" V_Stride => "); |
| 1097 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride); |
| 1098 | Debug.Put_Line (","); |
| 1099 | Debug.Put (" Tiling => "); |
| Nico Huber | fb80da9 | 2026-07-02 15:01:13 +0200 | [diff] [blame] | 1100 | Debug.Put (Tilings (Configs (Pipe).Framebuffer.Tiling)); |
| 1101 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 1102 | Debug.Put (" Rotation => "); |
| Nico Huber | fb80da9 | 2026-07-02 15:01:13 +0200 | [diff] [blame] | 1103 | Debug.Put (Rotations (Configs (Pipe).Framebuffer.Rotation)); |
| 1104 | Debug.Put_Line (","); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1105 | Debug.Put (" Offset => "); |
| 1106 | Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset); |
| 1107 | Debug.Put_Line (","); |
| 1108 | Debug.Put (" BPC => "); |
| 1109 | Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC); |
| 1110 | Debug.Put_Line ("),"); |
| 1111 | Debug.Put_Line (" Mode =>"); |
| 1112 | Debug.Put (" (Dotclock => "); |
| 1113 | Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock); |
| 1114 | Debug.Put_Line (","); |
| 1115 | Debug.Put (" H_Visible => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1116 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Visible); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1117 | Debug.Put_Line (","); |
| 1118 | Debug.Put (" H_Sync_Begin => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1119 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_Begin); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1120 | Debug.Put_Line (","); |
| 1121 | Debug.Put (" H_Sync_End => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1122 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_End); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1123 | Debug.Put_Line (","); |
| 1124 | Debug.Put (" H_Total => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1125 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Total); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1126 | Debug.Put_Line (","); |
| 1127 | Debug.Put (" V_Visible => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1128 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Visible); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1129 | Debug.Put_Line (","); |
| 1130 | Debug.Put (" V_Sync_Begin => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1131 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_Begin); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1132 | Debug.Put_Line (","); |
| 1133 | Debug.Put (" V_Sync_End => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1134 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_End); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1135 | Debug.Put_Line (","); |
| 1136 | Debug.Put (" V_Total => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 1137 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Total); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1138 | Debug.Put_Line (","); |
| Nico Huber | fb80da9 | 2026-07-02 15:01:13 +0200 | [diff] [blame] | 1139 | Debug.Put (" H_Sync_Active_High => "); |
| 1140 | if Configs (Pipe).Mode.H_Sync_Active_High then |
| 1141 | Debug.Put_Line ("True,"); |
| 1142 | else |
| 1143 | Debug.Put_Line ( "False,"); |
| 1144 | end if; |
| 1145 | Debug.Put (" V_Sync_Active_High => "); |
| 1146 | if Configs (Pipe).Mode.V_Sync_Active_High then |
| 1147 | Debug.Put_Line ("True,"); |
| 1148 | else |
| 1149 | Debug.Put_Line ("False,"); |
| 1150 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1151 | Debug.Put (" BPC => "); |
| 1152 | Debug.Put_Int64 (Configs (Pipe).Mode.BPC); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 1153 | if Pipe /= Pipe_Index'Last then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1154 | Debug.Put_Line (")),"); |
| 1155 | else |
| 1156 | Debug.Put_Line (")));"); |
| 1157 | end if; |
| 1158 | end loop; |
| 1159 | end Dump_Configs; |
| 1160 | |
| Nico Huber | c5c66ec | 2019-09-28 23:59:45 +0200 | [diff] [blame] | 1161 | ---------------------------------------------------------------------------- |
| 1162 | |
| 1163 | procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index) is |
| 1164 | begin |
| 1165 | Dev.Read16 (Value, Offset); |
| 1166 | end PCI_Read16; |
| 1167 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1168 | end HW.GFX.GMA; |