| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 1 | -- |
| 2 | -- Copyright (C) 2014-2016 secunet Security Networks AG |
| 3 | -- |
| 4 | -- This program is free software; you can redistribute it and/or modify |
| 5 | -- 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] | 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 8 | -- |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | -- |
| 14 | |
| 15 | with HW.GFX.EDID; |
| 16 | with HW.GFX.GMA.Config; |
| 17 | with HW.GFX.GMA.DP_Info; |
| 18 | with HW.GFX.GMA.Registers; |
| 19 | with HW.GFX.GMA.Power_And_Clocks; |
| 20 | with HW.GFX.GMA.Panel; |
| 21 | with HW.GFX.GMA.PLLs; |
| 22 | with HW.GFX.GMA.Port_Detect; |
| 23 | with HW.GFX.GMA.Connectors; |
| 24 | with HW.GFX.GMA.Connector_Info; |
| 25 | with HW.GFX.GMA.Pipe_Setup; |
| 26 | |
| 27 | with System; |
| 28 | |
| 29 | with HW.Debug; |
| 30 | with GNAT.Source_Info; |
| 31 | |
| 32 | use type HW.Word8; |
| 33 | use type HW.Int32; |
| 34 | |
| 35 | package body HW.GFX.GMA |
| 36 | with Refined_State => |
| 37 | (State => |
| 38 | (Registers.Address_State, |
| 39 | PLLs.State, Panel.Panel_State, |
| 40 | Cur_Configs, Allocated_PLLs, DP_Links, |
| 41 | HPD_Delay, Wait_For_HPD), |
| 42 | Init_State => Initialized, |
| 43 | Config_State => Config.Valid_Port_GPU, |
| 44 | Device_State => |
| 45 | (Registers.Register_State, Registers.GTT_State)) |
| 46 | is |
| 47 | |
| 48 | subtype Port_Name is String (1 .. 8); |
| 49 | type Port_Name_Array is array (Port_Type) of Port_Name; |
| 50 | Port_Names : constant Port_Name_Array := |
| 51 | (Disabled => "Disabled", |
| 52 | Internal => "Internal", |
| 53 | DP1 => "DP1 ", |
| 54 | DP2 => "DP2 ", |
| 55 | DP3 => "DP3 ", |
| 56 | Digital1 => "Digital1", |
| 57 | Digital2 => "Digital2", |
| 58 | Digital3 => "Digital3", |
| 59 | Analog => "Analog "); |
| 60 | |
| 61 | package Display_Controller renames Pipe_Setup; |
| 62 | |
| 63 | type PLLs_Type is array (Config_Index) of PLLs.T; |
| 64 | |
| 65 | type Links_Type is array (Config_Index) of DP_Link; |
| 66 | |
| 67 | type HPD_Type is array (Port_Type) of Boolean; |
| 68 | type HPD_Delay_Type is array (Port_Type) of Time.T; |
| 69 | |
| 70 | Cur_Configs : Configs_Type; |
| 71 | Allocated_PLLs : PLLs_Type; |
| 72 | DP_Links : Links_Type; |
| 73 | HPD_Delay : HPD_Delay_Type; |
| 74 | Wait_For_HPD : HPD_Type; |
| 75 | Initialized : Boolean := False; |
| 76 | |
| 77 | subtype Active_Port_Type is Port_Type range Port_Type'Succ (Disabled) .. Port_Type'Last; |
| 78 | |
| 79 | ---------------------------------------------------------------------------- |
| 80 | |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 81 | PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0; |
| 82 | |
| 83 | function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32 |
| 84 | is |
| 85 | begin |
| 86 | return Word32 (Freq / 1_000_000); |
| 87 | end PCH_RAWCLK_FREQ; |
| 88 | |
| 89 | ---------------------------------------------------------------------------- |
| 90 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 91 | function To_GPU_Port |
| 92 | (Configs : Configs_Type; |
| 93 | Idx : Config_Index) |
| 94 | return GPU_Port |
| 95 | is |
| 96 | begin |
| 97 | return |
| 98 | (case Config.CPU is |
| 99 | when Ironlake .. Ivybridge => -- everything but eDP through FDI/PCH |
| 100 | (if Config.Internal_Is_EDP and then Configs (Idx).Port = Internal |
| 101 | then |
| 102 | DIGI_A |
| 103 | else |
| 104 | (case Idx is |
| 105 | -- FDIs are fixed to the CPU pipe |
| 106 | when Primary => DIGI_B, |
| 107 | when Secondary => DIGI_C, |
| 108 | when Tertiary => DIGI_D)), |
| 109 | when Haswell .. Skylake => -- everything but VGA directly on CPU |
| 110 | (case Configs (Idx).Port is |
| 111 | when Disabled => GPU_Port'First, |
| 112 | when Internal => DIGI_A, -- LVDS not available |
| 113 | when Digital1 | DP1 => DIGI_B, |
| 114 | when Digital2 | DP2 => DIGI_C, |
| 115 | when Digital3 | DP3 => DIGI_D, |
| 116 | when Analog => DIGI_E)); |
| 117 | end To_GPU_Port; |
| 118 | |
| 119 | function To_PCH_Port (Port : Active_Port_Type) return PCH_Port |
| 120 | is |
| 121 | begin |
| 122 | return |
| 123 | (case Port is |
| 124 | when Internal => PCH_LVDS, -- will be ignored if Internal is DP |
| 125 | when Analog => PCH_DAC, |
| 126 | when Digital1 => PCH_HDMI_B, |
| 127 | when Digital2 => PCH_HDMI_C, |
| 128 | when Digital3 => PCH_HDMI_D, |
| 129 | when DP1 => PCH_DP_B, |
| 130 | when DP2 => PCH_DP_C, |
| 131 | when DP3 => PCH_DP_D); |
| 132 | end To_PCH_Port; |
| 133 | |
| 134 | function To_Display_Type (Port : Active_Port_Type) return Display_Type |
| 135 | with Pre => True |
| 136 | is |
| 137 | begin |
| 138 | return |
| 139 | (case Port is |
| 140 | when Internal => Config.Internal_Display, |
| 141 | when Analog => VGA, |
| 142 | when Digital1 | |
| 143 | Digital2 | |
| 144 | Digital3 => HDMI, |
| 145 | when DP1 | |
| 146 | DP2 | |
| 147 | DP3 => DP); |
| 148 | end To_Display_Type; |
| 149 | |
| 150 | procedure Configure_FDI_Link |
| 151 | (Port_Cfg : in out Port_Config; |
| 152 | Success : out Boolean) |
| 153 | with Pre => True |
| 154 | is |
| 155 | procedure Limit_Lane_Count |
| 156 | is |
| 157 | FDI_TX_CTL_FDI_TX_ENABLE : constant := 1 * 2 ** 31; |
| 158 | Enabled : Boolean; |
| 159 | begin |
| 160 | -- if DIGI_D enabled: (FDI names are off by one) |
| 161 | Registers.Is_Set_Mask |
| 162 | (Register => Registers.FDI_TX_CTL_C, |
| 163 | Mask => FDI_TX_CTL_FDI_TX_ENABLE, |
| 164 | Result => Enabled); |
| 165 | if Enabled then |
| 166 | Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count := DP_Lane_Count_2; |
| 167 | end if; |
| 168 | end Limit_Lane_Count; |
| 169 | begin |
| 170 | Port_Cfg.FDI.Receiver_Caps.Max_Link_Rate := DP_Bandwidth_2_7; |
| 171 | Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count := |
| 172 | Config.FDI_Lane_Count (Port_Cfg.Port); |
| 173 | Port_Cfg.FDI.Receiver_Caps.Enhanced_Framing := True; |
| 174 | if Config.Has_FDI_C and then Port_Cfg.Port = DIGI_C then |
| 175 | Limit_Lane_Count; |
| 176 | end if; |
| 177 | DP_Info.Preferred_Link_Setting (Port_Cfg.FDI, Port_Cfg.Mode, Success); |
| 178 | end Configure_FDI_Link; |
| 179 | |
| 180 | procedure Fill_Port_Config |
| 181 | (Port_Cfg : out Port_Config; |
| 182 | Configs : in Configs_Type; |
| 183 | Idx : in Config_Index; |
| 184 | Success : out Boolean) |
| 185 | with Pre => True |
| 186 | is |
| 187 | begin |
| 188 | Success := |
| 189 | Config.Supported_Pipe (Idx) and then |
| 190 | Config.Valid_Port (Configs (Idx).Port) and then |
| 191 | Configs (Idx).Port /= Disabled; |
| 192 | |
| 193 | if Success then |
| 194 | declare |
| 195 | Port : constant Port_Type := Configs (Idx).Port; |
| 196 | Mode : constant Mode_Type := Configs (Idx).Mode; |
| 197 | Link : constant DP_Link := DP_Links (Idx); |
| 198 | begin |
| 199 | Port_Cfg := Port_Config' |
| 200 | (Port => To_GPU_Port (Configs, Idx), |
| 201 | PCH_Port => To_PCH_Port (Port), |
| 202 | Display => To_Display_Type (Port), |
| 203 | Mode => Mode, |
| 204 | Is_FDI => Config.FDI_Port (To_GPU_Port (Configs, Idx)), |
| 205 | FDI => Default_DP, |
| 206 | DP => Link); |
| 207 | if Port_Cfg.Mode.BPC = Auto_BPC then |
| 208 | Port_Cfg.Mode.BPC := Connector_Info.Default_BPC (Port_Cfg); |
| 209 | end if; |
| 210 | end; |
| 211 | else |
| 212 | Port_Cfg := Port_Config' |
| 213 | (Port => GPU_Port'First, |
| 214 | PCH_Port => PCH_Port'First, |
| 215 | Display => Display_Type'First, |
| 216 | Mode => Invalid_Mode, |
| 217 | Is_FDI => False, |
| 218 | FDI => Default_DP, |
| 219 | DP => Default_DP); |
| 220 | end if; |
| 221 | end Fill_Port_Config; |
| 222 | |
| 223 | ---------------------------------------------------------------------------- |
| 224 | |
| 225 | function To_Controller |
| 226 | (Dsp_Config : Config_Index) return Display_Controller.Controller_Type |
| 227 | is |
| 228 | Result : Display_Controller.Controller_Type; |
| 229 | begin |
| 230 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 231 | |
| 232 | case Dsp_Config is |
| 233 | when Primary => |
| 234 | Result := Display_Controller.Controllers (Display_Controller.A); |
| 235 | when Secondary => |
| 236 | Result := Display_Controller.Controllers (Display_Controller.B); |
| 237 | when Tertiary => |
| 238 | Result := Display_Controller.Controllers (Display_Controller.C); |
| 239 | end case; |
| 240 | return Result; |
| 241 | end To_Controller; |
| 242 | |
| 243 | ---------------------------------------------------------------------------- |
| 244 | |
| 245 | function To_Head |
| 246 | (N_Config : Config_Index; |
| 247 | Port : Active_Port_Type) |
| 248 | return Display_Controller.Head_Type |
| 249 | is |
| 250 | Result : Display_Controller.Head_Type; |
| 251 | begin |
| 252 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 253 | |
| 254 | if Config.Has_EDP_Pipe and then Port = Internal then |
| 255 | Result := Display_Controller.Heads (Display_Controller.Head_EDP); |
| 256 | else |
| 257 | case N_Config is |
| 258 | when Primary => |
| 259 | Result := Display_Controller.Heads (Display_Controller.Head_A); |
| 260 | when Secondary => |
| 261 | Result := Display_Controller.Heads (Display_Controller.Head_B); |
| 262 | when Tertiary => |
| 263 | Result := Display_Controller.Heads (Display_Controller.Head_C); |
| 264 | end case; |
| 265 | end if; |
| 266 | return Result; |
| 267 | end To_Head; |
| 268 | |
| 269 | ---------------------------------------------------------------------------- |
| 270 | |
| 271 | procedure Legacy_VGA_Off |
| 272 | is |
| 273 | Reg8 : Word8; |
| 274 | begin |
| 275 | -- disable legacy VGA plane, taking over control now |
| 276 | Port_IO.OutB (VGA_SR_INDEX, VGA_SR01); |
| 277 | Port_IO.InB (Reg8, VGA_SR_DATA); |
| 278 | Port_IO.OutB (VGA_SR_DATA, Reg8 or 1 * 2 ** 5); |
| 279 | Time.U_Delay (100); -- PRM says 100us, Linux does 300 |
| 280 | Registers.Set_Mask (Registers.VGACNTRL, 1 * 2 ** 31); |
| 281 | end Legacy_VGA_Off; |
| 282 | |
| 283 | ---------------------------------------------------------------------------- |
| 284 | |
| 285 | function Port_Configured |
| 286 | (Configs : Configs_Type; |
| 287 | Port : Port_Type) |
| 288 | return Boolean |
| 289 | with |
| 290 | Global => null |
| 291 | is |
| 292 | begin |
| 293 | return Configs (Primary).Port = Port or |
| 294 | Configs (Secondary).Port = Port or |
| 295 | Configs (Tertiary).Port = Port; |
| 296 | end Port_Configured; |
| 297 | |
| 298 | procedure Scan_Ports |
| 299 | (Configs : out Configs_Type; |
| 300 | Ports : in Port_List) |
| 301 | is |
| 302 | Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#); |
| 303 | Port_Idx : Port_List_Range := Port_List_Range'First; |
| 304 | Port_Cfg : Port_Config; |
| 305 | Success : Boolean := False; |
| 306 | begin |
| 307 | Configs := (Config_Index => |
| 308 | (Port => Disabled, |
| 309 | Mode => Invalid_Mode, |
| 310 | Framebuffer => Default_FB)); |
| 311 | |
| 312 | for Config_Idx in Config_Index loop |
| 313 | while Ports (Port_Idx) /= Disabled loop |
| 314 | if not Port_Configured (Configs, Ports (Port_Idx)) then |
| 315 | Configs (Config_Idx).Port := Ports (Port_Idx); |
| 316 | Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success); |
| 317 | |
| 318 | if Success then |
| 319 | -- May need power to probe port |
| 320 | if Port_Cfg.Display = DP then |
| 321 | Power_And_Clocks.Power_Up (Cur_Configs, Configs); |
| 322 | end if; |
| 323 | if Ports (Port_Idx) = Internal then |
| 324 | Panel.On; |
| 325 | end if; |
| 326 | |
| 327 | Connector_Info.Read_EDID (Raw_EDID, Port_Cfg, Success); |
| 328 | end if; |
| 329 | |
| Nico Huber | 393aa8a | 2016-10-21 14:18:53 +0200 | [diff] [blame^] | 330 | if Success and then |
| 331 | (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and |
| 332 | EDID.Has_Preferred_Mode (Raw_EDID)) |
| 333 | then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 334 | Configs (Config_Idx).Mode := EDID.Preferred_Mode (Raw_EDID); |
| 335 | else |
| 336 | Configs (Config_Idx).Port := Disabled; |
| 337 | |
| 338 | if Ports (Port_Idx) = Internal and |
| 339 | not Port_Configured (Cur_Configs, Internal) |
| 340 | then |
| 341 | Panel.Off; |
| 342 | end if; |
| 343 | end if; |
| 344 | end if; |
| 345 | |
| 346 | exit when Port_Idx = Port_List_Range'Last; |
| 347 | Port_Idx := Port_List_Range'Succ (Port_Idx); |
| 348 | |
| 349 | exit when Success; |
| 350 | end loop; |
| 351 | end loop; |
| 352 | |
| 353 | Power_And_Clocks.Power_Set_To (Cur_Configs); |
| 354 | end Scan_Ports; |
| 355 | |
| 356 | procedure Auto_Configure |
| 357 | (Configs : in out Configs_Type; |
| 358 | Keep_Power : in Boolean := False) |
| 359 | is |
| 360 | Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#); |
| 361 | Success : Boolean; |
| 362 | |
| 363 | Config_Idx : Config_Index; |
| 364 | Port_Cfg : Port_Config; |
| 365 | |
| 366 | function Free_Config return Boolean |
| 367 | with |
| 368 | Pre => True |
| 369 | is |
| 370 | begin |
| 371 | return Port_Configured (Configs, Disabled); |
| 372 | end Free_Config; |
| 373 | |
| 374 | function First_Free_Config return Config_Index |
| 375 | with |
| 376 | Pre => Free_Config |
| 377 | is |
| 378 | begin |
| 379 | return (if Configs (Primary).Port = Disabled then Primary else |
| 380 | (if Configs (Secondary).Port = Disabled then Secondary |
| 381 | else Tertiary)); |
| 382 | end First_Free_Config; |
| 383 | begin |
| 384 | -- TODO: Only check ports with hot-plug event? |
| 385 | |
| 386 | if Config.Has_Internal_Display and then |
| 387 | not Keep_Power and then |
| 388 | not Port_Configured (Cur_Configs, Internal) |
| 389 | then |
| 390 | Panel.On (Wait => False); |
| 391 | end if; |
| 392 | |
| 393 | -- Check if displays are still connected |
| 394 | for I in Config_Index loop |
| 395 | if Configs (I).Port /= Disabled then |
| 396 | Fill_Port_Config (Port_Cfg, Configs, I, Success); |
| 397 | if Success then |
| 398 | Connector_Info.Read_EDID |
| 399 | (Raw_EDID => Raw_EDID, |
| 400 | Port_Cfg => Port_Cfg, |
| 401 | Success => Success); |
| 402 | end if; |
| 403 | if not Success or else |
| 404 | not EDID.Has_Preferred_Mode (Raw_EDID) or else |
| 405 | Configs (I).Mode /= EDID.Preferred_Mode (Raw_EDID) |
| 406 | then |
| 407 | Configs (I).Port := Disabled; |
| 408 | end if; |
| 409 | end if; |
| 410 | end loop; |
| 411 | |
| 412 | -- Add new displays as long as there is a free pipe config |
| 413 | for Port in Active_Port_Type loop |
| 414 | if Free_Config and then not Port_Configured (Configs, Port) then |
| 415 | Config_Idx := First_Free_Config; |
| 416 | Configs (Config_Idx).Port := Port; |
| 417 | Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success); |
| 418 | |
| 419 | if Success then |
| 420 | -- Need power to probe port |
| 421 | if not Keep_Power and then To_Display_Type (Port) = DP then |
| 422 | Power_And_Clocks.Power_Up (Cur_Configs, Configs); |
| 423 | end if; |
| 424 | if not Keep_Power and then Port = Internal then |
| 425 | Panel.Wait_On; |
| 426 | end if; |
| 427 | |
| 428 | Connector_Info.Read_EDID |
| 429 | (Raw_EDID => Raw_EDID, |
| 430 | Port_Cfg => Port_Cfg, |
| 431 | Success => Success); |
| 432 | end if; |
| 433 | |
| Nico Huber | 393aa8a | 2016-10-21 14:18:53 +0200 | [diff] [blame^] | 434 | if Success and then |
| 435 | (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and |
| 436 | EDID.Has_Preferred_Mode (Raw_EDID)) |
| 437 | then |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 438 | Configs (Config_Idx) := Config_Type' |
| 439 | (Port => Port, |
| 440 | Framebuffer => Configs (Config_Idx).Framebuffer, |
| 441 | Mode => EDID.Preferred_Mode (Raw_EDID)); |
| 442 | else |
| 443 | Configs (Config_Idx).Port := Disabled; |
| 444 | end if; |
| 445 | end if; |
| 446 | end loop; |
| 447 | |
| 448 | if not Keep_Power then |
| 449 | Power_And_Clocks.Power_Set_To (Cur_Configs); |
| 450 | |
| 451 | if Config.Has_Internal_Display and then |
| 452 | not Port_Configured (Cur_Configs, Internal) |
| 453 | then |
| 454 | Panel.Off; |
| 455 | end if; |
| 456 | end if; |
| 457 | end Auto_Configure; |
| 458 | |
| 459 | ---------------------------------------------------------------------------- |
| 460 | |
| 461 | procedure Update_Outputs (Configs : Configs_Type) |
| 462 | is |
| 463 | Did_Power_Up : Boolean := False; |
| 464 | |
| 465 | HPD, HPD_Delay_Over, Success : Boolean; |
| 466 | Old_Config, New_Config : Config_Type; |
| 467 | Old_Configs : Configs_Type; |
| 468 | Port_Cfg : Port_Config; |
| 469 | |
| 470 | procedure Check_HPD |
| 471 | (Port_Cfg : in Port_Config; |
| 472 | Port : in Port_Type; |
| 473 | Detected : out Boolean) |
| 474 | is |
| 475 | begin |
| 476 | HPD_Delay_Over := Time.Timed_Out (HPD_Delay (Port)); |
| 477 | if HPD_Delay_Over then |
| 478 | Port_Detect.Hotplug_Detect (Port_Cfg, Detected); |
| 479 | HPD_Delay (Port) := Time.MS_From_Now (333); |
| 480 | else |
| 481 | Detected := False; |
| 482 | end if; |
| 483 | end Check_HPD; |
| 484 | begin |
| 485 | Old_Configs := Cur_Configs; |
| 486 | |
| 487 | for I in Config_Index loop |
| 488 | HPD := False; |
| 489 | |
| 490 | Old_Config := Cur_Configs (I); |
| 491 | New_Config := Configs (I); |
| 492 | |
| 493 | Fill_Port_Config (Port_Cfg, Old_Configs, I, Success); |
| 494 | if Success then |
| 495 | Check_HPD (Port_Cfg, Old_Config.Port, HPD); |
| 496 | end if; |
| 497 | |
| 498 | -- Connector changed? |
| 499 | if (Success and then HPD) or |
| 500 | Old_Config.Port /= New_Config.Port or |
| 501 | Old_Config.Mode /= New_Config.Mode |
| 502 | then |
| 503 | if Old_Config.Port /= Disabled then |
| 504 | if Success then |
| 505 | pragma Debug (Debug.New_Line); |
| 506 | pragma Debug (Debug.Put_Line |
| 507 | ("Disabling port " & Port_Names (Old_Config.Port))); |
| 508 | |
| 509 | Connectors.Pre_Off (Port_Cfg); |
| 510 | |
| 511 | Display_Controller.Off |
| 512 | (To_Controller (I), To_Head (I, Old_Config.Port)); |
| 513 | |
| 514 | Connectors.Post_Off (Port_Cfg); |
| 515 | end if; |
| 516 | |
| 517 | -- Free PLL |
| 518 | PLLs.Free (Allocated_PLLs (I)); |
| 519 | |
| 520 | Cur_Configs (I).Port := Disabled; |
| 521 | end if; |
| 522 | |
| 523 | if New_Config.Port /= Disabled then |
| 524 | Fill_Port_Config (Port_Cfg, Configs, I, Success); |
| 525 | |
| 526 | if Success and then Wait_For_HPD (New_Config.Port) then |
| 527 | Check_HPD (Port_Cfg, New_Config.Port, Success); |
| 528 | Wait_For_HPD (New_Config.Port) := not Success; |
| 529 | end if; |
| 530 | |
| 531 | if Success then |
| 532 | pragma Debug (Debug.New_Line); |
| 533 | pragma Debug (Debug.Put_Line |
| 534 | ("Trying to enable port " & Port_Names (New_Config.Port))); |
| 535 | |
| 536 | if not Did_Power_Up then |
| 537 | Power_And_Clocks.Power_Up (Old_Configs, Configs); |
| 538 | Did_Power_Up := True; |
| 539 | end if; |
| 540 | |
| 541 | if Port_Cfg.Is_FDI then |
| 542 | Configure_FDI_Link (Port_Cfg, Success); |
| 543 | end if; |
| 544 | end if; |
| 545 | |
| 546 | if Success then |
| 547 | Connector_Info.Preferred_Link_Setting |
| 548 | (Port_Cfg => Port_Cfg, |
| 549 | Success => Success); |
| 550 | end if; |
| 551 | |
| 552 | while Success loop |
| 553 | pragma Loop_Invariant (New_Config.Port in Active_Port_Type); |
| 554 | |
| 555 | PLLs.Alloc |
| 556 | (Port_Cfg => Port_Cfg, |
| 557 | PLL => Allocated_PLLs (I), |
| 558 | Success => Success); |
| 559 | |
| 560 | if Success then |
| 561 | for Try in 1 .. 2 loop |
| 562 | pragma Loop_Invariant |
| 563 | (New_Config.Port in Active_Port_Type); |
| 564 | |
| 565 | Connectors.Pre_On |
| 566 | (Port_Cfg => Port_Cfg, |
| 567 | PLL_Hint => PLLs.Register_Value |
| 568 | (Allocated_PLLs (I)), |
| 569 | Pipe_Hint => Display_Controller.Get_Pipe_Hint |
| 570 | (To_Head (I, New_Config.Port)), |
| 571 | Success => Success); |
| 572 | |
| 573 | if Success then |
| 574 | Display_Controller.On |
| 575 | (Controller => To_Controller (I), |
| 576 | Head => To_Head (I, New_Config.Port), |
| 577 | Port_Cfg => Port_Cfg, |
| 578 | Framebuffer => New_Config.Framebuffer); |
| 579 | |
| 580 | Connectors.Post_On |
| 581 | (Port_Cfg => Port_Cfg, |
| 582 | PLL_Hint => PLLs.Register_Value |
| 583 | (Allocated_PLLs (I)), |
| 584 | Success => Success); |
| 585 | |
| 586 | if not Success then |
| 587 | Display_Controller.Off |
| 588 | (To_Controller (I), |
| 589 | To_Head (I, New_Config.Port)); |
| 590 | Connectors.Post_Off (Port_Cfg); |
| 591 | end if; |
| 592 | end if; |
| 593 | |
| 594 | exit when Success; |
| 595 | end loop; |
| 596 | exit when Success; -- connection established => stop loop |
| 597 | |
| 598 | -- connection failed |
| 599 | PLLs.Free (Allocated_PLLs (I)); |
| 600 | end if; |
| 601 | |
| 602 | Connector_Info.Next_Link_Setting |
| 603 | (Port_Cfg => Port_Cfg, |
| 604 | Success => Success); |
| 605 | end loop; |
| 606 | |
| 607 | if Success then |
| 608 | pragma Debug (Debug.Put_Line |
| 609 | ("Enabled port " & Port_Names (New_Config.Port))); |
| 610 | Cur_Configs (I) := New_Config; |
| 611 | DP_Links (I) := Port_Cfg.DP; |
| 612 | else |
| 613 | Wait_For_HPD (New_Config.Port) := True; |
| 614 | if New_Config.Port = Internal then |
| 615 | Panel.Off; |
| 616 | end if; |
| 617 | end if; |
| 618 | else |
| 619 | Cur_Configs (I) := New_Config; |
| 620 | end if; |
| 621 | elsif Old_Config.Framebuffer /= New_Config.Framebuffer and |
| 622 | Old_Config.Port /= Disabled |
| 623 | then |
| 624 | Display_Controller.Update_Offset |
| 625 | (Controller => To_Controller (I), |
| 626 | Framebuffer => New_Config.Framebuffer); |
| 627 | Cur_Configs (I) := New_Config; |
| 628 | end if; |
| 629 | end loop; |
| 630 | |
| 631 | if Did_Power_Up then |
| 632 | Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs); |
| 633 | end if; |
| 634 | |
| 635 | end Update_Outputs; |
| 636 | |
| 637 | ---------------------------------------------------------------------------- |
| 638 | |
| 639 | procedure Initialize |
| 640 | (MMIO_Base : in Word64 := 0; |
| 641 | Write_Delay : in Word64 := 0; |
| 642 | Success : out Boolean) |
| 643 | with |
| 644 | Refined_Global => |
| 645 | (In_Out => |
| 646 | (Config.Valid_Port_GPU, |
| 647 | Registers.Register_State, Port_IO.State), |
| 648 | Input => |
| 649 | (Time.State), |
| 650 | Output => |
| 651 | (Registers.Address_State, |
| 652 | PLLs.State, Panel.Panel_State, |
| 653 | Cur_Configs, Allocated_PLLs, DP_Links, |
| 654 | HPD_Delay, Wait_For_HPD, Initialized)) |
| 655 | is |
| 656 | use type HW.Word64; |
| 657 | |
| 658 | Now : constant Time.T := Time.Now; |
| 659 | |
| 660 | procedure Check_Platform (Success : out Boolean) |
| 661 | is |
| 662 | Audio_VID_DID : Word32; |
| 663 | begin |
| 664 | case Config.CPU is |
| 665 | when Haswell .. Skylake => |
| 666 | Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID); |
| 667 | when Ironlake .. Ivybridge => |
| 668 | Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID); |
| 669 | end case; |
| 670 | Success := |
| 671 | (case Config.CPU is |
| 672 | when Skylake => Audio_VID_DID = 16#8086_2809#, |
| 673 | when Broadwell => Audio_VID_DID = 16#8086_2808#, |
| 674 | when Haswell => Audio_VID_DID = 16#8086_2807#, |
| 675 | when Ivybridge | |
| 676 | Sandybridge => Audio_VID_DID = 16#8086_2806# or |
| 677 | Audio_VID_DID = 16#8086_2805#, |
| Nico Huber | eeb5a39 | 2016-10-09 19:28:30 +0200 | [diff] [blame] | 678 | when Ironlake => Audio_VID_DID = 16#0000_0000#); |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 679 | end Check_Platform; |
| 680 | begin |
| 681 | pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""", |
| 682 | Reason => "Write_Delay is used for debugging only"); |
| 683 | |
| 684 | pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity)); |
| 685 | |
| 686 | pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay)); |
| 687 | |
| 688 | Wait_For_HPD := HPD_Type'(others => False); |
| 689 | HPD_Delay := HPD_Delay_Type'(others => Now); |
| 690 | DP_Links := Links_Type'(others => HW.GFX.Default_DP); |
| 691 | Allocated_PLLs := (others => PLLs.Invalid); |
| 692 | Cur_Configs := Configs_Type' |
| 693 | (others => Config_Type' |
| 694 | (Port => Disabled, |
| 695 | Framebuffer => HW.GFX.Default_FB, |
| 696 | Mode => HW.GFX.Invalid_Mode)); |
| 697 | Registers.Set_Register_Base |
| 698 | (if MMIO_Base /= 0 then |
| 699 | MMIO_Base |
| 700 | else |
| 701 | Config.Default_MMIO_Base); |
| 702 | PLLs.Initialize; |
| 703 | |
| 704 | Check_Platform (Success); |
| 705 | if not Success then |
| 706 | pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH.")); |
| 707 | |
| 708 | Panel.Static_Init; -- for flow analysis |
| 709 | |
| 710 | Initialized := False; |
| 711 | return; |
| 712 | end if; |
| 713 | |
| 714 | Panel.Setup_PP_Sequencer; |
| 715 | Port_Detect.Initialize; |
| 716 | |
| 717 | Power_And_Clocks.Pre_All_Off; |
| 718 | |
| 719 | Legacy_VGA_Off; |
| 720 | |
| 721 | Connectors.Pre_All_Off; |
| 722 | Display_Controller.All_Off; |
| 723 | Connectors.Post_All_Off; |
| 724 | PLLs.All_Off; |
| 725 | |
| 726 | Power_And_Clocks.Post_All_Off; |
| 727 | |
| 728 | -------------------- Now restart from a clean state --------------------- |
| 729 | Power_And_Clocks.Initialize; |
| 730 | |
| Nico Huber | f54d096 | 2016-10-20 14:17:18 +0200 | [diff] [blame] | 731 | Registers.Unset_And_Set_Mask |
| 732 | (Register => Registers.PCH_RAWCLK_FREQ, |
| 733 | Mask_Unset => PCH_RAWCLK_FREQ_MASK, |
| 734 | Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq)); |
| 735 | |
| Nico Huber | 83693c8 | 2016-10-08 22:17:55 +0200 | [diff] [blame] | 736 | Initialized := True; |
| 737 | |
| 738 | end Initialize; |
| 739 | |
| 740 | function Is_Initialized return Boolean |
| 741 | with |
| 742 | Refined_Post => Is_Initialized'Result = Initialized |
| 743 | is |
| 744 | begin |
| 745 | return Initialized; |
| 746 | end Is_Initialized; |
| 747 | |
| 748 | ---------------------------------------------------------------------------- |
| 749 | |
| 750 | procedure Write_GTT |
| 751 | (GTT_Page : GTT_Range; |
| 752 | Device_Address : GTT_Address_Type; |
| 753 | Valid : Boolean) is |
| 754 | begin |
| 755 | Registers.Write_GTT (GTT_Page, Device_Address, Valid); |
| 756 | end Write_GTT; |
| 757 | |
| 758 | procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32) |
| 759 | is |
| 760 | FB_Size : constant Pos32 := |
| 761 | FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8)); |
| 762 | Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB); |
| 763 | begin |
| 764 | for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1) |
| 765 | loop |
| 766 | Registers.Write_GTT |
| 767 | (GTT_Page => Idx, |
| 768 | Device_Address => Phys_Addr, |
| 769 | Valid => True); |
| 770 | Phys_Addr := Phys_Addr + 4096; |
| 771 | end loop; |
| 772 | end Setup_Default_GTT; |
| 773 | |
| 774 | ---------------------------------------------------------------------------- |
| 775 | |
| 776 | procedure Dump_Configs (Configs : Configs_Type) |
| 777 | is |
| 778 | subtype Pipe_Name is String (1 .. 9); |
| 779 | type Pipe_Name_Array is array (Config_Index) of Pipe_Name; |
| 780 | Pipe_Names : constant Pipe_Name_Array := |
| 781 | (Primary => "Primary ", |
| 782 | Secondary => "Secondary", |
| 783 | Tertiary => "Tertiary "); |
| 784 | begin |
| 785 | Debug.New_Line; |
| 786 | Debug.Put_Line ("CONFIG => "); |
| 787 | for Pipe in Config_Index loop |
| 788 | if Pipe = Config_Index'First then |
| 789 | Debug.Put (" ("); |
| 790 | else |
| 791 | Debug.Put (" "); |
| 792 | end if; |
| 793 | Debug.Put_Line (Pipe_Names (Pipe) & " =>"); |
| 794 | Debug.Put_Line |
| 795 | (" (Port => " & Port_Names (Configs (Pipe).Port) & ","); |
| 796 | Debug.Put_Line (" Framebuffer =>"); |
| 797 | Debug.Put (" (Width => "); |
| 798 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width); |
| 799 | Debug.Put_Line (","); |
| 800 | Debug.Put (" Height => "); |
| 801 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height); |
| 802 | Debug.Put_Line (","); |
| 803 | Debug.Put (" Stride => "); |
| 804 | Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride); |
| 805 | Debug.Put_Line (","); |
| 806 | Debug.Put (" Offset => "); |
| 807 | Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset); |
| 808 | Debug.Put_Line (","); |
| 809 | Debug.Put (" BPC => "); |
| 810 | Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC); |
| 811 | Debug.Put_Line ("),"); |
| 812 | Debug.Put_Line (" Mode =>"); |
| 813 | Debug.Put (" (Dotclock => "); |
| 814 | Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock); |
| 815 | Debug.Put_Line (","); |
| 816 | Debug.Put (" H_Visible => "); |
| 817 | Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible); |
| 818 | Debug.Put_Line (","); |
| 819 | Debug.Put (" H_Sync_Begin => "); |
| 820 | Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin); |
| 821 | Debug.Put_Line (","); |
| 822 | Debug.Put (" H_Sync_End => "); |
| 823 | Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End); |
| 824 | Debug.Put_Line (","); |
| 825 | Debug.Put (" H_Total => "); |
| 826 | Debug.Put_Int16 (Configs (Pipe).Mode.H_Total); |
| 827 | Debug.Put_Line (","); |
| 828 | Debug.Put (" V_Visible => "); |
| 829 | Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible); |
| 830 | Debug.Put_Line (","); |
| 831 | Debug.Put (" V_Sync_Begin => "); |
| 832 | Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin); |
| 833 | Debug.Put_Line (","); |
| 834 | Debug.Put (" V_Sync_End => "); |
| 835 | Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End); |
| 836 | Debug.Put_Line (","); |
| 837 | Debug.Put (" V_Total => "); |
| 838 | Debug.Put_Int16 (Configs (Pipe).Mode.V_Total); |
| 839 | Debug.Put_Line (","); |
| 840 | Debug.Put_Line (" H_Sync_Active_High => " & |
| 841 | (if Configs (Pipe).Mode.H_Sync_Active_High |
| 842 | then "True," |
| 843 | else "False,")); |
| 844 | Debug.Put_Line (" V_Sync_Active_High => " & |
| 845 | (if Configs (Pipe).Mode.V_Sync_Active_High |
| 846 | then "True," |
| 847 | else "False,")); |
| 848 | Debug.Put (" BPC => "); |
| 849 | Debug.Put_Int64 (Configs (Pipe).Mode.BPC); |
| 850 | if Pipe /= Config_Index'Last then |
| 851 | Debug.Put_Line (")),"); |
| 852 | else |
| 853 | Debug.Put_Line (")));"); |
| 854 | end if; |
| 855 | end loop; |
| 856 | end Dump_Configs; |
| 857 | |
| 858 | end HW.GFX.GMA; |