| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <device/pci_ids.h> |
| 4 |
|
| 5 |
External (\_SB.PCI0.SPCO, MethodObj) |
| 6 |
|
| 7 |
|
| 8 |
/* Board specific defines */ |
| 9 |
|
| 10 |
/* PCI Subsystem ID to restore after wake */ |
| 11 |
#define GPU_SSID 0xa7611558 |
| 12 |
|
| 13 |
/* Power control signals */ |
| 14 |
#define GPIO_GPU_PERST_L GPP_B09 |
| 15 |
#define GPIO_GPU_PWR_EN GPP_D03 |
| 16 |
#define GPIO_GPU_PWR_GD GPP_C15 |
| 17 |
#define GPIO_GPU_NVVDD_EN GPP_F16 |
| 18 |
|
| 19 |
/* GPU clock pin */ |
| 20 |
#define GPU_SRCCLK_PIN 0x6 |
| 21 |
|
| 22 |
#define GC6_DEFER_TYPE_EXIT_GC6 3 |
| 23 |
|
| 24 |
/* 250ms in "Timer" units (i.e. 100ns increments) */ |
| 25 |
#define MIN_OFF_TIME_TIMERS 2500000 |
| 26 |
|
| 27 |
#define SRCCLK_DISABLE 0 |
| 28 |
#define SRCCLK_ENABLE 1 |
| 29 |
|
| 30 |
#define GPU_POWER_STATE_OFF 0 |
| 31 |
#define GPU_POWER_STATE_ON 1 |
| 32 |
|
| 33 |
/* Optimus Power Control State */ |
| 34 |
Name (OPCS, OPTIMUS_POWER_CONTROL_DISABLE) |
| 35 |
|
| 36 |
/* PCI configuration space Owner */ |
| 37 |
Name (PCIO, PCI_OWNER_SBIOS) |
| 38 |
|
| 39 |
/* Deferred GPU State */ |
| 40 |
Name (OPS0, OPTIMUS_CONTROL_NO_RUN_PS0) |
| 41 |
|
| 42 |
/* GC6 Entry/Exit state */ |
| 43 |
Name (GC6E, GC6_STATE_EXITED) |
| 44 |
|
| 45 |
/* Power State, GCOFF, GCON */ |
| 46 |
Name (GPPS, GPU_POWER_STATE_ON) |
| 47 |
|
| 48 |
/* Defer GC6 entry / exit until D3-cold request */ |
| 49 |
Name (DFEN, 0) |
| 50 |
/* Deferred GC6 Enter control */ |
| 51 |
Name (DFCI, 0) |
| 52 |
/* Deferred GC6 Exit control */ |
| 53 |
Name (DFCO, 0) |
| 54 |
/* GCOFF Timer */ |
| 55 |
Name (GCOT, 0) |
| 56 |
|
| 57 |
/* Copy of LTR enable bit from PEG port */ |
| 58 |
Name (SLTR, 0) |
| 59 |
|
| 60 |
/* Control the PCIe SRCCLK# for dGPU */ |
| 61 |
Method (SRCC, 1, Serialized) |
| 62 |
{ |
| 63 |
/* Control clock via P2SB interface */ |
| 64 |
\_SB.PCI0.SPCO (GPU_SRCCLK_PIN, Arg0) |
| 65 |
} |
| 66 |
|
| 67 |
/* "GC6 In", i.e. GC6 Entry Sequence */ |
| 68 |
Method (GC6I, 0, Serialized) |
| 69 |
{ |
| 70 |
GC6E = GC6_STATE_TRANSITION |
| 71 |
|
| 72 |
/* Save the PEG port's LTR setting */ |
| 73 |
SLTR = LREN |
| 74 |
|
| 75 |
/* Put PCIe link into L2/3 */ |
| 76 |
\_SB.PCI0.RP12.DL23 () |
| 77 |
|
| 78 |
/* Wait for GPU to deassert its GPIO4, i.e. GPU_NVVDD_EN */ |
| 79 |
GPPL (GPIO_GPU_NVVDD_EN, 0, 20) |
| 80 |
|
| 81 |
/* Assert GPU_PERST_L */ |
| 82 |
CTXS (GPIO_GPU_PERST_L) |
| 83 |
|
| 84 |
/* Disable PCIe SRCCLK# */ |
| 85 |
SRCC (SRCCLK_DISABLE) |
| 86 |
|
| 87 |
GC6E = GC6_STATE_ENTERED |
| 88 |
} |
| 89 |
|
| 90 |
/* "GC6 Out", i.e. GC6 Exit Sequence */ |
| 91 |
Method (GC6O, 0, Serialized) |
| 92 |
{ |
| 93 |
GC6E = GC6_STATE_TRANSITION |
| 94 |
|
| 95 |
/* Re-enable PCIe SRCCLK# */ |
| 96 |
SRCC (SRCCLK_ENABLE) |
| 97 |
|
| 98 |
/* Deassert GPU_PERST_L */ |
| 99 |
STXS (GPIO_GPU_PERST_L) |
| 100 |
|
| 101 |
/* Wait for GPU to assert GPU_NVVDD_EN */ |
| 102 |
GPPL (GPIO_GPU_NVVDD_EN, 1, 20) |
| 103 |
|
| 104 |
/* Restore PCIe link back to L0 state */ |
| 105 |
\_SB.PCI0.RP12.LD23 () |
| 106 |
|
| 107 |
/* Wait for dGPU to reappear on the bus */ |
| 108 |
Local0 = 50 |
| 109 |
While (NVID != PCI_VID_NVIDIA) |
| 110 |
{ |
| 111 |
Stall (100) |
| 112 |
Local0-- |
| 113 |
If (Local0 == 0) |
| 114 |
{ |
| 115 |
Break |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* Restore the PEG LTR enable bit */ |
| 120 |
LREN = SLTR |
| 121 |
|
| 122 |
/* Clear recoverable errors detected bit */ |
| 123 |
CEDR = 1 |
| 124 |
|
| 125 |
GC6E = GC6_STATE_EXITED |
| 126 |
} |
| 127 |
|
| 128 |
/* GCOFF exit sequence */ |
| 129 |
Method (PGON, 0, Serialized) |
| 130 |
{ |
| 131 |
Local0 = Timer - GCOT |
| 132 |
If (Local0 < MIN_OFF_TIME_TIMERS) |
| 133 |
{ |
| 134 |
Local1 = (MIN_OFF_TIME_TIMERS - Local0) / 10000 |
| 135 |
Printf("Sleeping %o to ensure min GCOFF time", Local1) |
| 136 |
Sleep (Local1) |
| 137 |
} |
| 138 |
|
| 139 |
/* Assert PERST# */ |
| 140 |
CTXS (GPIO_GPU_PERST_L) |
| 141 |
|
| 142 |
/* Enable power rails */ |
| 143 |
STXS (GPIO_GPU_PWR_EN) |
| 144 |
|
| 145 |
/* Wait 200ms for PWRGD */ |
| 146 |
GPPL (GPIO_GPU_PWR_GD, 1, 200) |
| 147 |
|
| 148 |
/* Deassert PERST# */ |
| 149 |
STXS (GPIO_GPU_PERST_L) |
| 150 |
|
| 151 |
GC6E = GC6_STATE_EXITED |
| 152 |
GPPS = GPU_POWER_STATE_ON |
| 153 |
} |
| 154 |
|
| 155 |
/* GCOFF entry sequence */ |
| 156 |
Method (PGOF, 0, Serialized) |
| 157 |
{ |
| 158 |
/* Assert PERST# */ |
| 159 |
CTXS (GPIO_GPU_PERST_L) |
| 160 |
|
| 161 |
/* Disable power rails */ |
| 162 |
CTXS (GPIO_GPU_PWR_EN) |
| 163 |
|
| 164 |
GCOT = Timer |
| 165 |
|
| 166 |
GPPS = GPU_POWER_STATE_OFF |
| 167 |
} |
| 168 |
|
| 169 |
/* GCOFF Out, i.e. full power-on sequence */ |
| 170 |
Method (GCOO, 0, Serialized) |
| 171 |
{ |
| 172 |
If (GPPS == GPU_POWER_STATE_ON) |
| 173 |
{ |
| 174 |
Printf ("PGON: GPU already on") |
| 175 |
Return |
| 176 |
} |
| 177 |
|
| 178 |
SRCC (SRCCLK_ENABLE) |
| 179 |
PGON () |
| 180 |
\_SB.PCI0.RP12.LD23 () |
| 181 |
|
| 182 |
/* Wait for dGPU to reappear on the bus */ |
| 183 |
Local0 = 50 |
| 184 |
While (NVID != PCI_VID_NVIDIA) |
| 185 |
{ |
| 186 |
Stall (100) |
| 187 |
Local0-- |
| 188 |
If (Local0 == 0) |
| 189 |
{ |
| 190 |
Break |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/* Restore the PEG LTR enable bit */ |
| 195 |
LREN = SLTR |
| 196 |
|
| 197 |
/* Clear recoverable errors detected bit */ |
| 198 |
CEDR = 1 |
| 199 |
|
| 200 |
/* Restore the PEG LTR enable bit */ |
| 201 |
LREN = SLTR |
| 202 |
|
| 203 |
/* Clear recoverable errors detected bit */ |
| 204 |
CEDR = 1 |
| 205 |
} |
| 206 |
|
| 207 |
/* GCOFF In, i.e. full power-off sequence */ |
| 208 |
Method (GCOI, 0, Serialized) |
| 209 |
{ |
| 210 |
If (GPPS == GPU_POWER_STATE_OFF) |
| 211 |
{ |
| 212 |
Printf ("GPU already off") |
| 213 |
Return |
| 214 |
} |
| 215 |
|
| 216 |
/* Save the PEG port's LTR setting */ |
| 217 |
SLTR = LREN |
| 218 |
\_SB.PCI0.RP12.DL23 () |
| 219 |
PGOF () |
| 220 |
SRCC (SRCCLK_DISABLE) |
| 221 |
} |
| 222 |
|
| 223 |
/* Handle deferred GC6 vs. poweron request */ |
| 224 |
Method (NPON, 0, Serialized) |
| 225 |
{ |
| 226 |
If (DFEN == GC6_DEFER_ENABLE) |
| 227 |
{ |
| 228 |
If (DFCO == GC6_DEFER_TYPE_EXIT_GC6) |
| 229 |
{ |
| 230 |
GC6O () |
| 231 |
} |
| 232 |
|
| 233 |
DFEN = GC6_DEFER_DISABLE |
| 234 |
} |
| 235 |
Else |
| 236 |
{ |
| 237 |
GCOO () |
| 238 |
} |
| 239 |
NVSS = GPU_SSID |
| 240 |
} |
| 241 |
|
| 242 |
/* Handle deferred GC6 vs. poweroff request */ |
| 243 |
Method (NPOF, 0, Serialized) |
| 244 |
{ |
| 245 |
/* Don't touch the `DFEN` flag until the GC6 exit. */ |
| 246 |
If (DFEN == GC6_DEFER_ENABLE) |
| 247 |
{ |
| 248 |
/* Deferred GC6 entry */ |
| 249 |
If (DFCI == NVJT_GPC_EGNS || DFCI == NVJT_GPC_EGIS) |
| 250 |
{ |
| 251 |
GC6I () |
| 252 |
} |
| 253 |
} |
| 254 |
Else |
| 255 |
{ |
| 256 |
GCOI () |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
Method (_ON, 0, Serialized) |
| 261 |
{ |
| 262 |
PGON () |
| 263 |
NVSS = GPU_SSID |
| 264 |
} |
| 265 |
|
| 266 |
Method (_OFF, 0, Serialized) |
| 267 |
{ |
| 268 |
PGOF () |
| 269 |
} |
| 270 |
|
| 271 |
/* Put device into D0 */ |
| 272 |
Method (_PS0, 0, NotSerialized) |
| 273 |
{ |
| 274 |
If (OPS0 == OPTIMUS_CONTROL_RUN_PS0) |
| 275 |
{ |
| 276 |
/* Poweron or deferred GC6 exit */ |
| 277 |
NPON () |
| 278 |
|
| 279 |
OPS0 = OPTIMUS_CONTROL_NO_RUN_PS0 |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/* Put device into D3 */ |
| 284 |
Method (_PS3, 0, NotSerialized) |
| 285 |
{ |
| 286 |
If (OPCS == OPTIMUS_POWER_CONTROL_ENABLE) |
| 287 |
{ |
| 288 |
/* Poweroff or deferred GC6 entry */ |
| 289 |
NPOF () |
| 290 |
|
| 291 |
/* Because _PS3 ran NPOF, _PS0 must run NPON */ |
| 292 |
OPS0 = OPTIMUS_CONTROL_RUN_PS0 |
| 293 |
|
| 294 |
/* OPCS is one-shot, so reset it */ |
| 295 |
OPCS = OPTIMUS_POWER_CONTROL_DISABLE |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
Method (PSTA, 0, Serialized) |
| 300 |
{ |
| 301 |
If (GC6E == GC6_STATE_EXITED && |
| 302 |
\_SB.PCI0.GTXS(GPIO_GPU_PWR_GD) == 1) |
| 303 |
{ |
| 304 |
Return (1) |
| 305 |
} |
| 306 |
Else |
| 307 |
{ |
| 308 |
Return (0) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
Method (_STA, 0, Serialized) |
| 313 |
{ |
| 314 |
Return (0xF) |
| 315 |
} |