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