| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1 | -- |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 2 | -- Copyright (C) 2014-2018 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; |
| 24 | with HW.GFX.GMA.Power_And_Clocks; |
| 25 | with HW.GFX.GMA.Panel; |
| 26 | with HW.GFX.GMA.PLLs; |
| 27 | with HW.GFX.GMA.Port_Detect; |
| 28 | with HW.GFX.GMA.Connectors; |
| 29 | with HW.GFX.GMA.Connector_Info; |
| 30 | with HW.GFX.GMA.Pipe_Setup; |
| 31 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 32 | with HW.Debug; |
| 33 | with GNAT.Source_Info; |
| 34 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 35 | use type HW.Int32; |
| 36 | |
| 37 | package body HW.GFX.GMA |
| 38 | with Refined_State => |
| 39 | (State => |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 40 | (Dev.Address_State, |
| 41 | Registers.Address_State, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 42 | PLLs.State, Panel.Panel_State, |
| Nico Huber | 1a712d3 | 2017-01-09 15:11:04 +0100 | [diff] [blame] | 43 | Cur_Configs, Allocated_PLLs, |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 44 | HPD_Delay, Wait_For_HPD, |
| 45 | Linear_FB_Base), |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 46 | Init_State => Initialized, |
| Nico Huber | 318bca1 | 2018-06-09 19:22:52 +0200 | [diff] [blame] | 47 | Config_State => (Config.Valid_Port, Config.Raw_Clock), |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 48 | Device_State => |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 49 | (Dev.PCI_State, Registers.Register_State, Registers.GTT_State)) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 50 | is |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 51 | pragma Disable_Atomic_Synchronization; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 52 | |
| 53 | subtype Port_Name is String (1 .. 8); |
| 54 | type Port_Name_Array is array (Port_Type) of Port_Name; |
| 55 | Port_Names : constant Port_Name_Array := |
| 56 | (Disabled => "Disabled", |
| 57 | Internal => "Internal", |
| 58 | DP1 => "DP1 ", |
| 59 | DP2 => "DP2 ", |
| 60 | DP3 => "DP3 ", |
| Nico Huber | 0d454cd | 2016-11-21 13:33:43 +0100 | [diff] [blame] | 61 | HDMI1 => "HDMI1 ", |
| 62 | HDMI2 => "HDMI2 ", |
| 63 | HDMI3 => "HDMI3 ", |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 64 | Analog => "Analog "); |
| 65 | |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 66 | package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0)); |
| 67 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 68 | package Display_Controller renames Pipe_Setup; |
| 69 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 70 | type PLLs_Type is array (Pipe_Index) of PLLs.T; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 71 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 72 | type HPD_Type is array (Port_Type) of Boolean; |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 73 | type HPD_Delay_Type is array (Active_Port_Type) of Time.T; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 74 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 75 | Allocated_PLLs : PLLs_Type; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 76 | HPD_Delay : HPD_Delay_Type; |
| 77 | Wait_For_HPD : HPD_Type; |
| 78 | Initialized : Boolean := False; |
| 79 | |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 80 | Linear_FB_Base : Word64; |
| 81 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 82 | ---------------------------------------------------------------------------- |
| 83 | |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 84 | PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0; |
| 85 | |
| 86 | function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32 |
| 87 | is |
| 88 | begin |
| 89 | return Word32 (Freq / 1_000_000); |
| 90 | end PCH_RAWCLK_FREQ; |
| 91 | |
| 92 | ---------------------------------------------------------------------------- |
| 93 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 94 | procedure Enable_Output |
| 95 | (Pipe : in Pipe_Index; |
| 96 | Pipe_Cfg : in Pipe_Config; |
| 97 | Success : out Boolean) |
| Nico Huber | 8a5a3b5 | 2018-06-04 14:42:13 +0200 | [diff] [blame] | 98 | with |
| 99 | Pre => Pipe_Cfg.Port in Active_Port_Type |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 100 | is |
| 101 | Port_Cfg : Port_Config; |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 102 | Scaler_Available : Boolean; |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 103 | begin |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 104 | pragma Debug (Debug.New_Line); |
| 105 | pragma Debug (Debug.Put_Line |
| 106 | ("Trying to enable port " & Port_Names (Pipe_Cfg.Port))); |
| 107 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 108 | Config_Helpers.Fill_Port_Config |
| 109 | (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success); |
| 110 | |
| 111 | if Success then |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 112 | Display_Controller.Scaler_Available (Scaler_Available, Pipe); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 113 | Success := Config_Helpers.Validate_Config |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 114 | (Pipe_Cfg.Framebuffer, Port_Cfg.Mode, Pipe, Scaler_Available); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 115 | end if; |
| 116 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 117 | if Success then |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 118 | Connector_Info.Preferred_Link_Setting (Port_Cfg, Success); |
| 119 | end if; |
| 120 | |
| 121 | -- loop over all possible DP-lane configurations |
| 122 | -- (non-DP ports use a single fake configuration) |
| 123 | while Success loop |
| 124 | pragma Loop_Invariant |
| 125 | (Pipe_Cfg.Port in Active_Port_Type and |
| 126 | Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry); |
| 127 | |
| 128 | PLLs.Alloc |
| 129 | (Port_Cfg => Port_Cfg, |
| 130 | PLL => Allocated_PLLs (Pipe), |
| 131 | Success => Success); |
| 132 | |
| 133 | if Success then |
| 134 | -- try each DP-lane configuration twice |
| 135 | for Try in 1 .. 2 loop |
| 136 | pragma Loop_Invariant |
| 137 | (Pipe_Cfg.Port in Active_Port_Type); |
| 138 | |
| Nico Huber | 4798c66 | 2017-01-11 12:44:48 +0100 | [diff] [blame] | 139 | -- Clear pending hot-plug events before every try |
| 140 | Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port); |
| 141 | |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 142 | Connectors.Pre_On |
| 143 | (Pipe => Pipe, |
| 144 | Port_Cfg => Port_Cfg, |
| 145 | PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)), |
| 146 | Success => Success); |
| 147 | |
| 148 | if Success then |
| 149 | Display_Controller.On |
| 150 | (Pipe => Pipe, |
| 151 | Port_Cfg => Port_Cfg, |
| Nico Huber | 4dc4c61 | 2018-01-10 15:55:09 +0100 | [diff] [blame] | 152 | Framebuffer => Pipe_Cfg.Framebuffer, |
| 153 | Cursor => Pipe_Cfg.Cursor); |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 154 | |
| 155 | Connectors.Post_On |
| Arthur Heymans | 60d0e5f | 2018-03-28 17:08:27 +0200 | [diff] [blame] | 156 | (Pipe => Pipe, |
| 157 | Port_Cfg => Port_Cfg, |
| Nico Huber | 43370ba | 2017-01-09 15:26:19 +0100 | [diff] [blame] | 158 | PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)), |
| 159 | Success => Success); |
| 160 | |
| 161 | if not Success then |
| 162 | Display_Controller.Off (Pipe); |
| 163 | Connectors.Post_Off (Port_Cfg); |
| 164 | end if; |
| 165 | end if; |
| 166 | |
| 167 | exit when Success; |
| 168 | end loop; |
| 169 | exit when Success; -- connection established => stop loop |
| 170 | |
| 171 | -- connection failed |
| 172 | PLLs.Free (Allocated_PLLs (Pipe)); |
| 173 | end if; |
| 174 | |
| 175 | Connector_Info.Next_Link_Setting (Port_Cfg, Success); |
| 176 | end loop; |
| 177 | |
| 178 | if Success then |
| 179 | pragma Debug (Debug.Put_Line |
| 180 | ("Enabled port " & Port_Names (Pipe_Cfg.Port))); |
| 181 | else |
| 182 | Wait_For_HPD (Pipe_Cfg.Port) := True; |
| 183 | if Pipe_Cfg.Port = Internal then |
| 184 | Panel.Off; |
| 185 | end if; |
| 186 | end if; |
| 187 | end Enable_Output; |
| 188 | |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 189 | procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config) |
| 190 | is |
| 191 | Port_Cfg : Port_Config; |
| 192 | Success : Boolean; |
| 193 | begin |
| 194 | Config_Helpers.Fill_Port_Config |
| 195 | (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success); |
| 196 | if Success then |
| 197 | pragma Debug (Debug.New_Line); |
| 198 | pragma Debug (Debug.Put_Line |
| 199 | ("Disabling port " & Port_Names (Pipe_Cfg.Port))); |
| 200 | pragma Debug (Debug.New_Line); |
| 201 | |
| 202 | Connectors.Pre_Off (Port_Cfg); |
| 203 | Display_Controller.Off (Pipe); |
| 204 | Connectors.Post_Off (Port_Cfg); |
| 205 | |
| 206 | PLLs.Free (Allocated_PLLs (Pipe)); |
| 207 | end if; |
| 208 | end Disable_Output; |
| 209 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 210 | procedure Update_Outputs (Configs : Pipe_Configs) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 211 | is |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 212 | procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean) |
| 213 | is |
| 214 | HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port)); |
| 215 | begin |
| 216 | if HPD_Delay_Over then |
| 217 | Port_Detect.Hotplug_Detect (Port, Detected); |
| 218 | HPD_Delay (Port) := Time.MS_From_Now (333); |
| 219 | else |
| 220 | Detected := False; |
| 221 | end if; |
| 222 | end Check_HPD; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 223 | |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 224 | Power_Changed : Boolean := False; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 225 | Old_Configs : Pipe_Configs; |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 226 | |
| 227 | -- Only called when we actually tried to change something |
| 228 | -- so we don't congest the log with unnecessary messages. |
| 229 | procedure Update_Power |
| 230 | is |
| 231 | begin |
| 232 | if not Power_Changed then |
| 233 | Power_And_Clocks.Power_Up (Old_Configs, Configs); |
| 234 | Power_Changed := True; |
| 235 | end if; |
| 236 | end Update_Power; |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 237 | |
| 238 | function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean |
| 239 | is |
| 240 | begin |
| 241 | return |
| Nico Huber | 958c564 | 2018-06-02 16:59:31 +0200 | [diff] [blame] | 242 | Cur_Config.Port /= New_Config.Port |
| 243 | or else |
| 244 | Cur_Config.Mode /= New_Config.Mode |
| 245 | or else |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 246 | (Config.Use_PDW_For_EDP_Scaling and then |
| 247 | (Cur_Config.Port = Internal and |
| Nico Huber | 958c564 | 2018-06-02 16:59:31 +0200 | [diff] [blame] | 248 | Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config))) |
| 249 | or else |
| 250 | (Config.Has_GMCH_PFIT_CONTROL and then |
| 251 | (Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config) or |
| 252 | Scaling_Type (Cur_Config) /= Scaling_Type (New_Config))); |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 253 | end Full_Update; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 254 | begin |
| 255 | Old_Configs := Cur_Configs; |
| 256 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 257 | -- disable all pipes that changed or had a hot-plug event |
| 258 | for Pipe in Pipe_Index loop |
| 259 | declare |
| 260 | Unplug_Detected : Boolean; |
| 261 | Cur_Config : Pipe_Config renames Cur_Configs (Pipe); |
| 262 | New_Config : Pipe_Config renames Configs (Pipe); |
| 263 | begin |
| 264 | if Cur_Config.Port /= Disabled then |
| 265 | Check_HPD (Cur_Config.Port, Unplug_Detected); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 266 | |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 267 | if Full_Update (Cur_Config, New_Config) or Unplug_Detected then |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 268 | Disable_Output (Pipe, Cur_Config); |
| 269 | Cur_Config.Port := Disabled; |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 270 | Update_Power; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 271 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 272 | end if; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 273 | end; |
| 274 | end loop; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 275 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 276 | -- enable all pipes that changed and should be active |
| 277 | for Pipe in Pipe_Index loop |
| 278 | declare |
| 279 | Success : Boolean; |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 280 | Scaler_Available : Boolean; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 281 | Cur_Config : Pipe_Config renames Cur_Configs (Pipe); |
| 282 | New_Config : Pipe_Config renames Configs (Pipe); |
| 283 | begin |
| Nico Huber | 3d06de8 | 2018-05-29 01:35:04 +0200 | [diff] [blame] | 284 | if New_Config.Port /= Disabled and |
| 285 | Full_Update (Cur_Config, New_Config) |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 286 | then |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 287 | if Wait_For_HPD (New_Config.Port) then |
| 288 | Check_HPD (New_Config.Port, Success); |
| 289 | Wait_For_HPD (New_Config.Port) := not Success; |
| 290 | else |
| 291 | Success := True; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 292 | end if; |
| Nico Huber | c7a4fee | 2016-11-03 18:18:03 +0100 | [diff] [blame] | 293 | |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 294 | if Success then |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 295 | Update_Power; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 296 | Enable_Output (Pipe, New_Config, Success); |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 297 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 298 | |
| 299 | if Success then |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 300 | Cur_Config := New_Config; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 301 | end if; |
| Nico Huber | 3be61d4 | 2017-01-09 13:58:18 +0100 | [diff] [blame] | 302 | |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 303 | -- update framebuffer offset only |
| 304 | elsif New_Config.Port /= Disabled and |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 305 | Cur_Config.Framebuffer /= New_Config.Framebuffer |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 306 | then |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 307 | Display_Controller.Scaler_Available (Scaler_Available, Pipe); |
| 308 | if Config_Helpers.Validate_Config |
| 309 | (New_Config.Framebuffer, New_Config.Mode, |
| 310 | Pipe, Scaler_Available) |
| 311 | then |
| 312 | Display_Controller.Setup_FB |
| 313 | (Pipe, New_Config.Mode, New_Config.Framebuffer); |
| 314 | Display_Controller.Update_Cursor |
| 315 | (Pipe, New_Config.Framebuffer, New_Config.Cursor); |
| 316 | Cur_Config := New_Config; |
| 317 | end if; |
| Nico Huber | b56b9c5 | 2017-01-11 15:12:23 +0100 | [diff] [blame] | 318 | end if; |
| 319 | end; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 320 | end loop; |
| 321 | |
| Nico Huber | 564103f | 2017-01-11 15:33:07 +0100 | [diff] [blame] | 322 | if Power_Changed then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 323 | Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs); |
| 324 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 325 | end Update_Outputs; |
| 326 | |
| 327 | ---------------------------------------------------------------------------- |
| 328 | |
| Nico Huber | 15ffc4f | 2018-01-11 14:44:43 +0100 | [diff] [blame] | 329 | procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type) |
| 330 | is |
| 331 | begin |
| 332 | Cur_Configs (Pipe).Cursor := Cursor; |
| 333 | Display_Controller.Update_Cursor |
| 334 | (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor); |
| 335 | end Update_Cursor; |
| 336 | |
| 337 | procedure Place_Cursor |
| 338 | (Pipe : Pipe_Index; |
| 339 | X : Cursor_Pos; |
| 340 | Y : Cursor_Pos) |
| 341 | is |
| 342 | begin |
| 343 | Cur_Configs (Pipe).Cursor.Center_X := X; |
| 344 | Cur_Configs (Pipe).Cursor.Center_Y := Y; |
| 345 | Display_Controller.Place_Cursor |
| 346 | (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor); |
| 347 | end Place_Cursor; |
| 348 | |
| 349 | procedure Move_Cursor |
| 350 | (Pipe : Pipe_Index; |
| 351 | X : Cursor_Pos; |
| 352 | Y : Cursor_Pos) |
| 353 | is |
| 354 | function Cap_Add (A, B : Cursor_Pos) return Cursor_Pos is |
| 355 | (if A + B < 0 |
| 356 | then Int32'Max (Cursor_Pos'First, A + B) |
| 357 | else Int32'Min (Cursor_Pos'Last, A + B)); |
| 358 | begin |
| 359 | Place_Cursor |
| 360 | (Pipe => Pipe, |
| 361 | X => Cap_Add (Cur_Configs (Pipe).Cursor.Center_X, X), |
| 362 | Y => Cap_Add (Cur_Configs (Pipe).Cursor.Center_Y, Y)); |
| 363 | end Move_Cursor; |
| 364 | |
| 365 | ---------------------------------------------------------------------------- |
| 366 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 367 | procedure Initialize |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 368 | (Write_Delay : in Word64 := 0; |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 369 | Clean_State : in Boolean := False; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 370 | Success : out Boolean) |
| 371 | with |
| 372 | Refined_Global => |
| 373 | (In_Out => |
| Nico Huber | 318bca1 | 2018-06-09 19:22:52 +0200 | [diff] [blame] | 374 | (Config.Valid_Port, Dev.PCI_State, |
| Arthur Heymans | d1988d1 | 2018-03-28 16:27:57 +0200 | [diff] [blame] | 375 | Registers.Register_State, Port_IO.State, |
| 376 | Config.Raw_Clock), |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 377 | Input => |
| 378 | (Time.State), |
| 379 | Output => |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 380 | (Dev.Address_State, |
| 381 | Registers.Address_State, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 382 | PLLs.State, Panel.Panel_State, |
| Nico Huber | 1a712d3 | 2017-01-09 15:11:04 +0100 | [diff] [blame] | 383 | Cur_Configs, Allocated_PLLs, |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 384 | HPD_Delay, Wait_For_HPD, |
| 385 | Linear_FB_Base, Initialized)) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 386 | is |
| 387 | use type HW.Word64; |
| 388 | |
| Nico Huber | 0b2329a | 2018-06-09 21:14:27 +0200 | [diff] [blame] | 389 | function MMIO_GTT_Offset return Natural is |
| 390 | (if Config.Has_64bit_GTT |
| 391 | then Registers.MMIO_GTT_64_Offset |
| 392 | else Registers.MMIO_GTT_32_Offset); |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 393 | PCI_MMIO_Base, PCI_GTT_Base : Word64; |
| 394 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 395 | Now : constant Time.T := Time.Now; |
| 396 | |
| 397 | procedure Check_Platform (Success : out Boolean) |
| 398 | is |
| 399 | Audio_VID_DID : Word32; |
| 400 | begin |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame^] | 401 | case Config.Gen is |
| Arthur Heymans | 73ea032 | 2018-03-28 17:17:07 +0200 | [diff] [blame] | 402 | when G45 => |
| 403 | Registers.Read (Registers.G4X_AUD_VID_DID, Audio_VID_DID); |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame^] | 404 | when Ironlake => |
| 405 | Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 406 | when Haswell .. Skylake => |
| 407 | Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 408 | end case; |
| 409 | Success := |
| 410 | (case Config.CPU is |
| Nico Huber | 21da574 | 2017-01-20 14:00:53 +0100 | [diff] [blame] | 411 | when Broxton => Audio_VID_DID = 16#8086_280a#, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 412 | when Skylake => Audio_VID_DID = 16#8086_2809#, |
| 413 | when Broadwell => Audio_VID_DID = 16#8086_2808#, |
| 414 | when Haswell => Audio_VID_DID = 16#8086_2807#, |
| 415 | when Ivybridge | |
| 416 | Sandybridge => Audio_VID_DID = 16#8086_2806# or |
| 417 | Audio_VID_DID = 16#8086_2805#, |
| Arthur Heymans | 73ea032 | 2018-03-28 17:17:07 +0200 | [diff] [blame] | 418 | when Ironlake => Audio_VID_DID = 16#0000_0000#, |
| 419 | when G45 => Audio_VID_DID = 16#8086_2801# or |
| 420 | Audio_VID_DID = 16#8086_2802# or |
| 421 | Audio_VID_DID = 16#8086_2803#); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 422 | end Check_Platform; |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 423 | |
| 424 | procedure Check_Platform_PCI (Success : out Boolean) |
| 425 | is |
| 426 | use type HW.Word16; |
| 427 | Vendor, Device : Word16; |
| 428 | begin |
| 429 | Dev.Read16 (Vendor, PCI.Vendor_Id); |
| 430 | Dev.Read16 (Device, PCI.Device_Id); |
| 431 | |
| 432 | Success := Vendor = 16#8086# and Config.Compatible_GPU (Device); |
| 433 | end Check_Platform_PCI; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 434 | begin |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 435 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 436 | |
| 437 | pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay)); |
| 438 | |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 439 | Linear_FB_Base := 0; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 440 | Wait_For_HPD := HPD_Type'(others => False); |
| 441 | HPD_Delay := HPD_Delay_Type'(others => Now); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 442 | Allocated_PLLs := (others => PLLs.Invalid); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 443 | Cur_Configs := Pipe_Configs' |
| 444 | (others => Pipe_Config' |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 445 | (Port => Disabled, |
| 446 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 447 | Cursor => Default_Cursor, |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 448 | Mode => HW.GFX.Invalid_Mode)); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 449 | PLLs.Initialize; |
| 450 | |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 451 | Dev.Initialize (Success); |
| 452 | |
| 453 | if Success then |
| Nico Huber | 0b2329a | 2018-06-09 21:14:27 +0200 | [diff] [blame] | 454 | Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => MMIO_GTT_Offset); |
| 455 | Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => MMIO_GTT_Offset); |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 456 | if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then |
| 457 | Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base); |
| 458 | else |
| 459 | pragma Debug (Debug.Put_Line |
| 460 | ("ERROR: Couldn't map resoure0.")); |
| 461 | Registers.Set_Register_Base (Config.Default_MMIO_Base); |
| 462 | Success := Config.Default_MMIO_Base_Set; |
| 463 | end if; |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 464 | |
| 465 | if Success then |
| 466 | Check_Platform_PCI (Success); |
| 467 | end if; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 468 | else |
| 469 | pragma Debug (Debug.Put_Line |
| 470 | ("WARNING: Couldn't initialize PCI dev.")); |
| 471 | Registers.Set_Register_Base (Config.Default_MMIO_Base); |
| 472 | Success := Config.Default_MMIO_Base_Set; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 473 | |
| Nico Huber | e7ac6eb | 2017-09-04 23:54:13 +0200 | [diff] [blame] | 474 | if Success then |
| 475 | Check_Platform (Success); |
| 476 | end if; |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 477 | end if; |
| 478 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 479 | if not Success then |
| 480 | pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH.")); |
| 481 | |
| 482 | Panel.Static_Init; -- for flow analysis |
| 483 | |
| 484 | Initialized := False; |
| 485 | return; |
| 486 | end if; |
| 487 | |
| 488 | Panel.Setup_PP_Sequencer; |
| 489 | Port_Detect.Initialize; |
| Nico Huber | 0923b79 | 2017-06-09 15:28:41 +0200 | [diff] [blame] | 490 | Connectors.Initialize; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 491 | |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 492 | if Clean_State then |
| 493 | Power_And_Clocks.Pre_All_Off; |
| 494 | Connectors.Pre_All_Off; |
| 495 | Display_Controller.All_Off; |
| 496 | Connectors.Post_All_Off; |
| 497 | PLLs.All_Off; |
| 498 | Power_And_Clocks.Post_All_Off; |
| Nico Huber | 17d64b6 | 2017-07-15 20:51:25 +0200 | [diff] [blame] | 499 | Registers.Clear_Fences; |
| Nico Huber | 33912aa | 2016-12-06 20:36:23 +0100 | [diff] [blame] | 500 | else |
| 501 | -- According to PRMs, VGA plane is the only thing |
| Nico Huber | 3a0e2a0 | 2017-07-19 14:41:46 +0200 | [diff] [blame] | 502 | -- that's enabled by default after reset... |
| Nico Huber | 33912aa | 2016-12-06 20:36:23 +0100 | [diff] [blame] | 503 | Display_Controller.Legacy_VGA_Off; |
| Nico Huber | 3a0e2a0 | 2017-07-19 14:41:46 +0200 | [diff] [blame] | 504 | -- ... along with some DDI port bits since Skylake. |
| 505 | Connectors.Post_Reset_Off; |
| Nico Huber | 793a8d4 | 2016-11-21 18:57:03 +0100 | [diff] [blame] | 506 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 507 | |
| 508 | -------------------- Now restart from a clean state --------------------- |
| 509 | Power_And_Clocks.Initialize; |
| 510 | |
| Nico Huber | 1c3b928 | 2017-02-09 13:57:04 +0100 | [diff] [blame] | 511 | if Config.Has_PCH then |
| 512 | Registers.Unset_And_Set_Mask |
| 513 | (Register => Registers.PCH_RAWCLK_FREQ, |
| 514 | Mask_Unset => PCH_RAWCLK_FREQ_MASK, |
| 515 | Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq)); |
| 516 | end if; |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 517 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 518 | Initialized := True; |
| 519 | |
| 520 | end Initialize; |
| 521 | |
| 522 | function Is_Initialized return Boolean |
| 523 | with |
| 524 | Refined_Post => Is_Initialized'Result = Initialized |
| 525 | is |
| 526 | begin |
| 527 | return Initialized; |
| 528 | end Is_Initialized; |
| 529 | |
| 530 | ---------------------------------------------------------------------------- |
| 531 | |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 532 | pragma Warnings |
| 533 | (GNATprove, Off, """Registers.Register_State"" * is not modified*", |
| 534 | Reason => "Power_Up_VGA is only effective on certain generations."); |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 535 | procedure Power_Up_VGA |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 536 | with |
| 537 | Refined_Global => |
| 538 | (Input => (Cur_Configs, Time.State), |
| 539 | In_Out => (Registers.Register_State), |
| 540 | Proof_In => (Initialized)) |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 541 | is |
| 542 | Fake_Config : constant Pipe_Configs := |
| 543 | (Primary => |
| 544 | (Port => Analog, |
| 545 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 546 | Cursor => Default_Cursor, |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 547 | Mode => HW.GFX.Invalid_Mode), |
| 548 | others => |
| 549 | (Port => Disabled, |
| 550 | Framebuffer => HW.GFX.Default_FB, |
| Nico Huber | a02b2c6 | 2018-01-09 15:58:34 +0100 | [diff] [blame] | 551 | Cursor => Default_Cursor, |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 552 | Mode => HW.GFX.Invalid_Mode)); |
| 553 | begin |
| 554 | Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config); |
| 555 | end Power_Up_VGA; |
| Nico Huber | cf88f3d | 2018-06-05 13:27:34 +0200 | [diff] [blame] | 556 | pragma Warnings |
| 557 | (GNATprove, Off, "no check message justified*", Reason => "see below"); |
| 558 | pragma Annotate |
| 559 | (GNATprove, Intentional, "unused global", |
| 560 | "Power_Up_VGA is only effective on certain generations."); |
| 561 | pragma Warnings (GNATprove, On, "no check message justified*"); |
| 562 | pragma Warnings |
| 563 | (GNATprove, On, """Registers.Register_State"" * is not modified*"); |
| Nico Huber | 42fb2d0 | 2017-09-01 17:01:51 +0200 | [diff] [blame] | 564 | |
| 565 | ---------------------------------------------------------------------------- |
| 566 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 567 | function FB_First_Page (FB : Framebuffer_Type) return Natural is |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 568 | (Natural (Phys_Offset (FB) / GTT_Page_Size)); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 569 | function FB_Pages (FB : Framebuffer_Type) return Natural is |
| 570 | (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size))); |
| 571 | function FB_Last_Page (FB : Framebuffer_Type) return Natural is |
| 572 | (FB_First_Page (FB) + FB_Pages (FB) - 1); |
| 573 | |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 574 | -- Check basics and that it fits in GTT. For 90 degree rotations, |
| 575 | -- the Offset should be above GTT_Rotation_Offset. The latter will |
| 576 | -- be subtracted for the aperture mapping. |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 577 | function Valid_FB (FB : Framebuffer_Type) return Boolean is |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 578 | (Valid_Stride (FB) and |
| 579 | FB_First_Page (FB) in GTT_Range and |
| 580 | FB_Last_Page (FB) in GTT_Range and |
| 581 | (not Rotation_90 (FB) or |
| 582 | (FB_Last_Page (FB) + GTT_Rotation_Offset in GTT_Range and |
| 583 | FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size))); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 584 | |
| 585 | -- Also check that we don't overflow the GTT's 39-bit space |
| 586 | -- (always true with a 32-bit base) |
| 587 | function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32) |
| 588 | return Boolean is |
| 589 | (Valid_FB (FB) and |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 590 | Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <= |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 591 | Int64 (GTT_Address_Type'Last)) |
| 592 | with |
| 593 | Ghost; |
| 594 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 595 | procedure Write_GTT |
| 596 | (GTT_Page : GTT_Range; |
| 597 | Device_Address : GTT_Address_Type; |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 598 | Valid : Boolean) |
| 599 | is |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 600 | begin |
| 601 | Registers.Write_GTT (GTT_Page, Device_Address, Valid); |
| 602 | end Write_GTT; |
| 603 | |
| Nico Huber | ceda17d | 2018-06-09 22:00:29 +0200 | [diff] [blame] | 604 | procedure Read_GTT |
| 605 | (Device_Address : out GTT_Address_Type; |
| 606 | Valid : out Boolean; |
| 607 | GTT_Page : in GTT_Range) |
| 608 | is |
| 609 | begin |
| 610 | Registers.Read_GTT (Device_Address, Valid, GTT_Page); |
| 611 | end Read_GTT; |
| 612 | |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 613 | procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32) |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 614 | with |
| 615 | Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 616 | is |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 617 | Phys_Addr : GTT_Address_Type := |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 618 | GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB)); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 619 | begin |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 620 | for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 621 | Registers.Write_GTT |
| 622 | (GTT_Page => Idx, |
| 623 | Device_Address => Phys_Addr, |
| 624 | Valid => True); |
| Nico Huber | 194e57e | 2017-07-15 21:15:46 +0200 | [diff] [blame] | 625 | Phys_Addr := Phys_Addr + GTT_Page_Size; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 626 | end loop; |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 627 | |
| 628 | if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then |
| 629 | declare |
| 630 | V_Pages : constant Natural := Natural (FB.V_Stride) / 32; |
| 631 | Bytes_Per_Row : constant GTT_Address_Type := |
| 632 | GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB)); |
| 633 | begin |
| 634 | Phys_Addr := GTT_Address_Type (Phys_Base) + |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 635 | GTT_Address_Type (Phys_Offset (FB)) + |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 636 | GTT_Address_Type (FB_Size (FB)); |
| 637 | for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop |
| 638 | Phys_Addr := Phys_Addr - Bytes_Per_Row; |
| 639 | Registers.Write_GTT |
| 640 | (GTT_Page => GTT_Rotation_Offset + Page, |
| 641 | Device_Address => Phys_Addr, |
| 642 | Valid => True); |
| 643 | |
| 644 | if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then |
| 645 | Phys_Addr := Phys_Addr + GTT_Page_Size + |
| 646 | GTT_Address_Type (V_Pages) * Bytes_Per_Row; |
| 647 | end if; |
| 648 | end loop; |
| 649 | end; |
| 650 | end if; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 651 | end Setup_Default_GTT; |
| 652 | |
| 653 | ---------------------------------------------------------------------------- |
| 654 | |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 655 | use type HW.Word16; |
| 656 | subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33; |
| 657 | |
| 658 | function GGMS_Gen4 (GGC : Word16) return Natural is |
| 659 | (Natural (Shift_Right (GGC, 8) and 16#07#)); |
| 660 | function GTT_Size_Gen4 (GGC : Word16) return Natural is |
| 661 | (if GGMS_Gen4 (GGC) in 1 .. 3 then |
| 662 | (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0); |
| 663 | |
| 664 | function GMS_Gen4 (GGC : Word16) return Natural is |
| 665 | (Natural (Shift_Right (GGC, 4) and 16#0f#)); |
| 666 | Valid_Stolen_Size_Gen4 : constant |
| 667 | array (Natural range 1 .. 13) of Stolen_Size_Range := |
| 668 | (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352); |
| 669 | function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is |
| 670 | (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then |
| Arthur Heymans | 5fd9a31 | 2017-09-12 12:45:18 +0200 | [diff] [blame] | 671 | Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0); |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 672 | |
| 673 | function GTT_Size_Gen6 (GGC : Word16) return Natural is |
| 674 | (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20); |
| 675 | |
| 676 | function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is |
| 677 | (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20); |
| 678 | |
| 679 | function GTT_Size_Gen8 (GGC : Word16) return Natural is |
| 680 | (Natural (Shift_Right (GGC, 6) and 16#03#) * 2 ** 20); |
| 681 | |
| 682 | function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is |
| 683 | (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#)); |
| 684 | function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is |
| 685 | (GMS_Gen8 (GGC) * 32 * 2 ** 20); |
| 686 | |
| 687 | function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is |
| 688 | (if GMS_Gen8 (GGC) < 16#f0# then |
| 689 | Stolen_Size_Gen8 (GGC) |
| 690 | else |
| 691 | (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20); |
| 692 | |
| 693 | procedure Decode_Stolen |
| 694 | (GTT_Size : out Natural; |
| 695 | Stolen_Size : out Stolen_Size_Range) |
| 696 | with |
| 697 | Pre => Is_Initialized |
| 698 | is |
| 699 | GGC_Reg : constant := |
| 700 | (case Config.CPU is |
| Arthur Heymans | 73ea032 | 2018-03-28 17:17:07 +0200 | [diff] [blame] | 701 | when G45 | Ironlake => 16#52#, |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 702 | when Sandybridge .. Skylake => 16#50#); |
| 703 | GGC : Word16; |
| 704 | begin |
| 705 | Dev.Read16 (GGC, GGC_Reg); |
| 706 | case Config.CPU is |
| Arthur Heymans | 73ea032 | 2018-03-28 17:17:07 +0200 | [diff] [blame] | 707 | when G45 | Ironlake => |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 708 | GTT_Size := GTT_Size_Gen4 (GGC); |
| 709 | Stolen_Size := Stolen_Size_Gen4 (GGC); |
| 710 | when Sandybridge .. Haswell => |
| 711 | GTT_Size := GTT_Size_Gen6 (GGC); |
| 712 | Stolen_Size := Stolen_Size_Gen6 (GGC); |
| 713 | when Broadwell => |
| 714 | GTT_Size := GTT_Size_Gen8 (GGC); |
| 715 | Stolen_Size := Stolen_Size_Gen8 (GGC); |
| 716 | when Broxton .. Skylake => |
| 717 | GTT_Size := GTT_Size_Gen8 (GGC); |
| 718 | Stolen_Size := Stolen_Size_Gen9 (GGC); |
| 719 | end case; |
| 720 | end Decode_Stolen; |
| 721 | |
| 722 | -- Additional runtime validation that FB fits stolen memory and aperture. |
| 723 | procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean) |
| 724 | with |
| 725 | Pre => Is_Initialized, |
| 726 | Post => (if Valid then Valid_FB (FB)) |
| 727 | is |
| 728 | GTT_Size, Aperture_Size : Natural; |
| 729 | Stolen_Size : Stolen_Size_Range; |
| 730 | begin |
| 731 | Valid := Valid_FB (FB); |
| 732 | |
| 733 | if Valid then |
| 734 | Decode_Stolen (GTT_Size, Stolen_Size); |
| 735 | Dev.Resource_Size (Aperture_Size, PCI.Res2); |
| 736 | Valid := |
| 737 | FB_Last_Page (FB) < GTT_Size / Config.GTT_PTE_Size and |
| 738 | FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size) and |
| 739 | FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size; |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 740 | pragma Debug (not Valid, Debug.Put_Line |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 741 | ("Stolen memory too small to hold framebuffer.")); |
| 742 | end if; |
| 743 | end Validate_FB; |
| 744 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 745 | procedure Setup_Default_FB |
| 746 | (FB : in Framebuffer_Type; |
| 747 | Clear : in Boolean := True; |
| 748 | Success : out Boolean) |
| 749 | is |
| 750 | GMA_Phys_Base : constant PCI.Index := 16#5c#; |
| 751 | GMA_Phys_Base_Mask : constant := 16#fff0_0000#; |
| 752 | |
| 753 | Phys_Base : Word32; |
| 754 | begin |
| Nico Huber | eedde88 | 2017-07-16 02:54:39 +0200 | [diff] [blame] | 755 | Validate_FB (FB, Success); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 756 | |
| 757 | if Success then |
| 758 | Dev.Read32 (Phys_Base, GMA_Phys_Base); |
| 759 | Phys_Base := Phys_Base and GMA_Phys_Base_Mask; |
| 760 | Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0; |
| 761 | pragma Debug (not Success, Debug.Put_Line |
| 762 | ("Failed to read stolen memory base.")); |
| Nico Huber | 0164b02 | 2017-08-24 15:12:51 +0200 | [diff] [blame] | 763 | |
| 764 | if Success then |
| 765 | if FB.Tiling in XY_Tiling then |
| 766 | Registers.Add_Fence |
| 767 | (First_Page => FB_First_Page (FB), |
| 768 | Last_Page => FB_Last_Page (FB), |
| 769 | Tiling => FB.Tiling, |
| 770 | Pitch => FB_Pitch (FB.Stride, FB), |
| 771 | Success => Success); |
| 772 | end if; |
| 773 | pragma Debug (not Success, Debug.Put_Line |
| 774 | ("Tiled framebuffer but no fence regs available.")); |
| 775 | end if; |
| 776 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 777 | if Success then |
| 778 | Setup_Default_GTT (FB, Phys_Base); |
| 779 | end if; |
| 780 | end if; |
| 781 | |
| 782 | if Success and then Clear then |
| 783 | declare |
| 784 | use type HW.Word64; |
| 785 | Linear_FB : Word64; |
| 786 | begin |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 787 | Map_Linear_FB (Linear_FB, FB); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 788 | if Linear_FB /= 0 then |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 789 | Framebuffer_Filler.Fill (Linear_FB, FB); |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 790 | end if; |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 791 | end; |
| 792 | end if; |
| 793 | end Setup_Default_FB; |
| 794 | |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 795 | procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type) |
| 796 | is |
| 797 | use type HW.Word64; |
| 798 | |
| 799 | Valid : Boolean; |
| 800 | begin |
| 801 | Linear_FB := 0; |
| 802 | |
| 803 | if Linear_FB_Base = 0 then |
| 804 | Dev.Map (Linear_FB_Base, PCI.Res2); |
| 805 | pragma Debug |
| 806 | (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2.")); |
| 807 | end if; |
| 808 | |
| 809 | if Linear_FB_Base /= 0 then |
| 810 | Validate_FB (FB, Valid); |
| 811 | if Valid then |
| Nico Huber | 34be654 | 2017-12-13 09:26:24 +0100 | [diff] [blame] | 812 | Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB)); |
| Nico Huber | c3f66f6 | 2017-07-16 21:39:54 +0200 | [diff] [blame] | 813 | end if; |
| 814 | end if; |
| 815 | end Map_Linear_FB; |
| 816 | |
| Nico Huber | 5374c3a | 2017-07-15 21:48:06 +0200 | [diff] [blame] | 817 | ---------------------------------------------------------------------------- |
| 818 | |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 819 | procedure Dump_Configs (Configs : Pipe_Configs) |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 820 | is |
| 821 | subtype Pipe_Name is String (1 .. 9); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 822 | type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name; |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 823 | Pipe_Names : constant Pipe_Name_Array := |
| 824 | (Primary => "Primary ", |
| 825 | Secondary => "Secondary", |
| 826 | Tertiary => "Tertiary "); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 827 | |
| 828 | subtype Tiling_Name is String (1 .. 7); |
| 829 | type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name; |
| 830 | Tilings : constant Tiling_Name_Array := |
| 831 | (Linear => "Linear ", |
| 832 | X_Tiled => "X_Tiled", |
| 833 | Y_Tiled => "Y_Tiled"); |
| 834 | |
| 835 | subtype Rotation_Name is String (1 .. 11); |
| 836 | type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name; |
| 837 | Rotations : constant Rotation_Name_Array := |
| 838 | (No_Rotation => "No_Rotation", |
| 839 | Rotated_90 => "Rotated_90 ", |
| 840 | Rotated_180 => "Rotated_180", |
| 841 | Rotated_270 => "Rotated_270"); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 842 | begin |
| 843 | Debug.New_Line; |
| Paul Menzel | b83107c | 2017-05-04 09:02:33 +0200 | [diff] [blame] | 844 | Debug.Put_Line ("CONFIG =>"); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 845 | for Pipe in Pipe_Index loop |
| 846 | if Pipe = Pipe_Index'First then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 847 | Debug.Put (" ("); |
| 848 | else |
| 849 | Debug.Put (" "); |
| 850 | end if; |
| 851 | Debug.Put_Line (Pipe_Names (Pipe) & " =>"); |
| 852 | Debug.Put_Line |
| 853 | (" (Port => " & Port_Names (Configs (Pipe).Port) & ","); |
| 854 | Debug.Put_Line (" Framebuffer =>"); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 855 | Debug.Put (" (Width => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 856 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width); |
| 857 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 858 | Debug.Put (" Height => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 859 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height); |
| 860 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 861 | Debug.Put (" Start_X => "); |
| 862 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X); |
| 863 | Debug.Put_Line (","); |
| 864 | Debug.Put (" Start_Y => "); |
| 865 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y); |
| 866 | Debug.Put_Line (","); |
| 867 | Debug.Put (" Stride => "); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 868 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride); |
| 869 | Debug.Put_Line (","); |
| Nico Huber | 5ef4d60 | 2017-12-13 13:56:47 +0100 | [diff] [blame] | 870 | Debug.Put (" V_Stride => "); |
| 871 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride); |
| 872 | Debug.Put_Line (","); |
| 873 | Debug.Put (" Tiling => "); |
| 874 | Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ","); |
| 875 | Debug.Put (" Rotation => "); |
| 876 | Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ","); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 877 | Debug.Put (" Offset => "); |
| 878 | Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset); |
| 879 | Debug.Put_Line (","); |
| 880 | Debug.Put (" BPC => "); |
| 881 | Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC); |
| 882 | Debug.Put_Line ("),"); |
| 883 | Debug.Put_Line (" Mode =>"); |
| 884 | Debug.Put (" (Dotclock => "); |
| 885 | Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock); |
| 886 | Debug.Put_Line (","); |
| 887 | Debug.Put (" H_Visible => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 888 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Visible); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 889 | Debug.Put_Line (","); |
| 890 | Debug.Put (" H_Sync_Begin => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 891 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_Begin); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 892 | Debug.Put_Line (","); |
| 893 | Debug.Put (" H_Sync_End => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 894 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_End); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 895 | Debug.Put_Line (","); |
| 896 | Debug.Put (" H_Total => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 897 | Debug.Put_Int32 (Configs (Pipe).Mode.H_Total); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 898 | Debug.Put_Line (","); |
| 899 | Debug.Put (" V_Visible => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 900 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Visible); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 901 | Debug.Put_Line (","); |
| 902 | Debug.Put (" V_Sync_Begin => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 903 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_Begin); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 904 | Debug.Put_Line (","); |
| 905 | Debug.Put (" V_Sync_End => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 906 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_End); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 907 | Debug.Put_Line (","); |
| 908 | Debug.Put (" V_Total => "); |
| Nico Huber | c5c767a | 2018-06-03 01:09:04 +0200 | [diff] [blame] | 909 | Debug.Put_Int32 (Configs (Pipe).Mode.V_Total); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 910 | Debug.Put_Line (","); |
| 911 | Debug.Put_Line (" H_Sync_Active_High => " & |
| 912 | (if Configs (Pipe).Mode.H_Sync_Active_High |
| 913 | then "True," |
| 914 | else "False,")); |
| 915 | Debug.Put_Line (" V_Sync_Active_High => " & |
| 916 | (if Configs (Pipe).Mode.V_Sync_Active_High |
| 917 | then "True," |
| 918 | else "False,")); |
| 919 | Debug.Put (" BPC => "); |
| 920 | Debug.Put_Int64 (Configs (Pipe).Mode.BPC); |
| Nico Huber | 99f10f3 | 2016-11-20 00:34:05 +0100 | [diff] [blame] | 921 | if Pipe /= Pipe_Index'Last then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 922 | Debug.Put_Line (")),"); |
| 923 | else |
| 924 | Debug.Put_Line (")));"); |
| 925 | end if; |
| 926 | end loop; |
| 927 | end Dump_Configs; |
| 928 | |
| 929 | end HW.GFX.GMA; |