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