| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 1 | -- |
| Nico Huber | 88da05e | 2019-09-16 21:03:20 +0200 | [diff] [blame] | 2 | -- Copyright (C) 2015-2016, 2019 secunet Security Networks AG |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 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 |
| 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| 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.GMA.Config; |
| 16 | with HW.GFX.GMA.Connector_Info; |
| 17 | with HW.GFX.GMA.DP_Info; |
| 18 | with HW.GFX.GMA.Registers; |
| 19 | |
| 20 | with HW.Debug; |
| 21 | |
| 22 | package body HW.GFX.GMA.Config_Helpers |
| 23 | is |
| 24 | |
| 25 | function To_GPU_Port |
| 26 | (Pipe : Pipe_Index; |
| 27 | Port : Active_Port_Type) |
| 28 | return GPU_Port |
| 29 | is |
| 30 | begin |
| 31 | return |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame] | 32 | (case Config.Gen is |
| 33 | when G45 => -- everything on GMCH |
| Arthur Heymans | 73ea032 | 2018-03-28 17:17:07 +0200 | [diff] [blame] | 34 | (case Port is |
| 35 | when Internal => LVDS, |
| 36 | when HDMI1 | DP1 => DIGI_B, |
| 37 | when HDMI2 | DP2 => DIGI_C, |
| 38 | when HDMI3 | DP3 => DIGI_D, |
| 39 | when Analog => VGA), |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame] | 40 | when Ironlake => -- everything but eDP through FDI/PCH |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 41 | (if Config.Internal_Is_EDP and then Port = Internal then |
| 42 | DIGI_A |
| 43 | else |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame] | 44 | (case Pipe is -- FDIs are fixed to the CPU pipe |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 45 | when Primary => DIGI_B, |
| 46 | when Secondary => DIGI_C, |
| 47 | when Tertiary => DIGI_D)), |
| Nico Huber | 6621a14 | 2018-06-07 23:56:54 +0200 | [diff] [blame] | 48 | when others => -- everything but VGA directly on CPU |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 49 | (case Port is |
| 50 | when Internal => DIGI_A, -- LVDS not available |
| 51 | when HDMI1 | DP1 => DIGI_B, |
| 52 | when HDMI2 | DP2 => DIGI_C, |
| 53 | when HDMI3 | DP3 => DIGI_D, |
| 54 | when Analog => DIGI_E)); |
| 55 | end To_GPU_Port; |
| 56 | |
| 57 | function To_PCH_Port (Port : Active_Port_Type) return PCH_Port |
| 58 | is |
| 59 | begin |
| 60 | return |
| 61 | (case Port is |
| 62 | when Internal => PCH_LVDS, -- will be ignored if Internal is DP |
| 63 | when Analog => PCH_DAC, |
| 64 | when HDMI1 => PCH_HDMI_B, |
| 65 | when HDMI2 => PCH_HDMI_C, |
| 66 | when HDMI3 => PCH_HDMI_D, |
| 67 | when DP1 => PCH_DP_B, |
| 68 | when DP2 => PCH_DP_C, |
| 69 | when DP3 => PCH_DP_D); |
| 70 | end To_PCH_Port; |
| 71 | |
| 72 | function To_Display_Type (Port : Active_Port_Type) return Display_Type |
| 73 | is |
| 74 | begin |
| 75 | return Display_Type' |
| 76 | (case Port is |
| 77 | when Internal => Config.Internal_Display, |
| 78 | when Analog => VGA, |
| 79 | when HDMI1 .. HDMI3 => HDMI, |
| 80 | when DP1 .. DP3 => DP); |
| 81 | end To_Display_Type; |
| 82 | |
| Nico Huber | 88da05e | 2019-09-16 21:03:20 +0200 | [diff] [blame] | 83 | function Highest_Dotclock (Configs : Pipe_Configs) return Frequency_Type |
| 84 | is |
| 85 | Max : Frequency_Type := Frequency_Type'First; |
| 86 | begin |
| 87 | for I in Pipe_Index loop |
| 88 | if Configs (I).Port /= Disabled and |
| 89 | Max < Configs (I).Mode.Dotclock |
| 90 | then |
| 91 | Max := Configs (I).Mode.Dotclock; |
| 92 | end if; |
| 93 | end loop; |
| 94 | return Max; |
| 95 | end Highest_Dotclock; |
| 96 | |
| 97 | procedure Limit_Dotclocks |
| 98 | (Configs : in out Pipe_Configs; |
| 99 | Max : in Frequency_Type) is |
| 100 | begin |
| 101 | for I in Pipe_Index loop |
| 102 | if Configs (I).Port /= Disabled and |
| 103 | Max < Configs (I).Mode.Dotclock |
| 104 | then |
| 105 | Configs (I).Mode.Dotclock := Max; |
| 106 | end if; |
| 107 | end loop; |
| 108 | end Limit_Dotclocks; |
| 109 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 110 | ---------------------------------------------------------------------------- |
| 111 | |
| 112 | -- Prepares link rate and lane count settings for an FDI connection. |
| 113 | procedure Configure_FDI_Link |
| 114 | (Port_Cfg : in out Port_Config; |
| 115 | Success : out Boolean) |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame^] | 116 | with |
| 117 | Post => |
| 118 | Port_Cfg.Mode.H_Visible = Port_Cfg'Old.Mode.H_Visible and |
| 119 | Port_Cfg.Mode.V_Visible = Port_Cfg'Old.Mode.V_Visible |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 120 | is |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame^] | 121 | FDI_TX_CTL_FDI_TX_ENABLE : constant := 1 * 2 ** 31; |
| 122 | Enabled : Boolean; |
| 123 | begin |
| 124 | Port_Cfg.FDI.Receiver_Caps.Max_Link_Rate := DP_Bandwidth_2_7; |
| 125 | Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count := |
| 126 | Config.FDI_Lane_Count (Port_Cfg.Port); |
| 127 | Port_Cfg.FDI.Receiver_Caps.Enhanced_Framing := True; |
| 128 | |
| 129 | if Config.Has_FDI_C and then Port_Cfg.Port = DIGI_C then |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 130 | -- if DIGI_D enabled: (FDI names are off by one) |
| 131 | Registers.Is_Set_Mask |
| 132 | (Register => Registers.FDI_TX_CTL_C, |
| 133 | Mask => FDI_TX_CTL_FDI_TX_ENABLE, |
| 134 | Result => Enabled); |
| 135 | if Enabled then |
| 136 | Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count := DP_Lane_Count_2; |
| 137 | end if; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 138 | end if; |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame^] | 139 | |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 140 | DP_Info.Preferred_Link_Setting (Port_Cfg.FDI, Port_Cfg.Mode, Success); |
| 141 | end Configure_FDI_Link; |
| 142 | |
| 143 | -- Derives an internal port config. |
| 144 | -- |
| 145 | -- This is where the magic happens that hides the hardware details |
| 146 | -- from libgfxinit's users. We have to map the pipe (Pipe_Index), |
| 147 | -- the user visible port (Port_Type) and the modeline (Mode_Type) |
| 148 | -- that we are supposed to output to an internal representation |
| 149 | -- (Port_Config) that applies to the selected hardware generation |
| 150 | -- (in GMA.Config). |
| 151 | procedure Fill_Port_Config |
| 152 | (Port_Cfg : out Port_Config; |
| 153 | Pipe : in Pipe_Index; |
| 154 | Port : in Port_Type; |
| 155 | Mode : in Mode_Type; |
| 156 | Success : out Boolean) |
| 157 | is |
| 158 | begin |
| 159 | Success := |
| Nico Huber | d58de7d | 2018-06-07 23:06:55 +0200 | [diff] [blame] | 160 | (Config.Has_Tertiary_Pipe or Pipe <= Secondary) and then |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 161 | Config.Valid_Port (Port) and then |
| 162 | Port /= Disabled; -- Valid_Port should already cover this, but the |
| 163 | -- array is writeable, so it's hard to prove this. |
| 164 | |
| 165 | if Success then |
| 166 | Port_Cfg := Port_Config' |
| 167 | (Port => To_GPU_Port (Pipe, Port), |
| 168 | PCH_Port => To_PCH_Port (Port), |
| 169 | Display => To_Display_Type (Port), |
| 170 | Mode => Mode, |
| 171 | Is_FDI => Config.Is_FDI_Port (Port), |
| 172 | FDI => Default_DP, |
| 173 | DP => Default_DP); |
| 174 | |
| Nico Huber | 02cfbb3 | 2017-01-09 17:41:18 +0100 | [diff] [blame] | 175 | if Port_Cfg.Mode.BPC = Auto_BPC then |
| 176 | Port_Cfg.Mode.BPC := Connector_Info.Default_BPC (Port_Cfg); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 177 | end if; |
| 178 | |
| Nico Huber | 02cfbb3 | 2017-01-09 17:41:18 +0100 | [diff] [blame] | 179 | if Port_Cfg.Display = HDMI then |
| 180 | declare |
| 181 | pragma Assert (Config.HDMI_Max_Clock_24bpp * 8 |
| 182 | / Port_Cfg.Mode.BPC >= Frequency_Type'First); |
| 183 | Max_Dotclock : constant Frequency_Type := |
| 184 | Config.HDMI_Max_Clock_24bpp * 8 / Port_Cfg.Mode.BPC; |
| 185 | begin |
| 186 | if Port_Cfg.Mode.Dotclock > Max_Dotclock then |
| 187 | pragma Debug (Debug.Put ("Dotclock ")); |
| 188 | pragma Debug (Debug.Put_Int64 (Port_Cfg.Mode.Dotclock)); |
| 189 | pragma Debug (Debug.Put (" too high, limiting to ")); |
| 190 | pragma Debug (Debug.Put_Int64 (Max_Dotclock)); |
| 191 | pragma Debug (Debug.Put_Line (".")); |
| 192 | Port_Cfg.Mode.Dotclock := Max_Dotclock; |
| 193 | end if; |
| 194 | end; |
| 195 | end if; |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 196 | |
| Nico Huber | 02cfbb3 | 2017-01-09 17:41:18 +0100 | [diff] [blame] | 197 | if Port_Cfg.Is_FDI then |
| 198 | Configure_FDI_Link (Port_Cfg, Success); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 199 | end if; |
| 200 | else |
| 201 | Port_Cfg := Port_Config' |
| 202 | (Port => GPU_Port'First, |
| 203 | PCH_Port => PCH_Port'First, |
| 204 | Display => Display_Type'First, |
| 205 | Mode => Invalid_Mode, |
| 206 | Is_FDI => False, |
| 207 | FDI => Default_DP, |
| 208 | DP => Default_DP); |
| 209 | end if; |
| 210 | end Fill_Port_Config; |
| 211 | |
| 212 | ---------------------------------------------------------------------------- |
| 213 | |
| 214 | -- Validates that a given configuration should work with |
| 215 | -- a given framebuffer. |
| 216 | function Validate_Config |
| Nico Huber | f361ec8 | 2018-06-02 18:01:45 +0200 | [diff] [blame] | 217 | (FB : Framebuffer_Type; |
| 218 | Mode : Mode_Type; |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame^] | 219 | Pipe : Pipe_Index) |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 220 | return Boolean |
| 221 | is |
| 222 | begin |
| 223 | -- No downscaling |
| 224 | -- Respect maximum scalable width |
| 225 | -- VGA plane is only allowed on the primary pipe |
| 226 | -- Only 32bpp RGB (ignored for VGA plane) |
| Nico Huber | 0164b02 | 2017-08-24 15:12:51 +0200 | [diff] [blame] | 227 | -- Stride must be big enough and a multiple of 64 bytes or the tile size |
| 228 | -- (ignored for VGA plane) |
| Nico Huber | ab69e36 | 2018-05-29 21:20:30 +0200 | [diff] [blame] | 229 | -- Y-Tiling and rotation are only supported on newer generations (with |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 230 | -- Plane_Control) |
| 231 | -- 90 degree rotations are only supported with Y-tiling |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 232 | return |
| Nico Huber | cbbaade | 2018-01-02 13:59:36 +0100 | [diff] [blame] | 233 | ((Rotated_Width (FB) = Mode.H_Visible and |
| 234 | Rotated_Height (FB) = Mode.V_Visible) or |
| Nico Huber | 9a4c4c3 | 2019-09-16 22:05:11 +0200 | [diff] [blame^] | 235 | (Rotated_Width (FB) <= Config.Maximum_Scalable_Width (Pipe) and |
| Nico Huber | cbbaade | 2018-01-02 13:59:36 +0100 | [diff] [blame] | 236 | Rotated_Width (FB) <= Mode.H_Visible and |
| 237 | Rotated_Height (FB) <= Mode.V_Visible)) and |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 238 | (FB.Offset /= VGA_PLANE_FRAMEBUFFER_OFFSET or Pipe = Primary) and |
| 239 | (FB.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET or |
| 240 | (FB.BPC = 8 and Valid_Stride (FB) and |
| 241 | (Config.Has_Plane_Control or |
| Nico Huber | ab69e36 | 2018-05-29 21:20:30 +0200 | [diff] [blame] | 242 | (FB.Tiling /= Y_Tiled and FB.Rotation = No_Rotation)) and |
| Nico Huber | 9b47941 | 2017-08-27 11:55:56 +0200 | [diff] [blame] | 243 | (FB.Tiling = Y_Tiled or not Rotation_90 (FB)))); |
| Nico Huber | 8c45bcf | 2016-11-20 17:30:57 +0100 | [diff] [blame] | 244 | end Validate_Config; |
| 245 | |
| 246 | end HW.GFX.GMA.Config_Helpers; |