nouveau_acpi.c revision 1.1.1.3 1 /* $NetBSD: nouveau_acpi.c,v 1.1.1.3 2021/12/18 20:15:35 riastradh Exp $ */
2
3 // SPDX-License-Identifier: MIT
4 #include <sys/cdefs.h>
5 __KERNEL_RCSID(0, "$NetBSD: nouveau_acpi.c,v 1.1.1.3 2021/12/18 20:15:35 riastradh Exp $");
6
7 #include <linux/pci.h>
8 #include <linux/acpi.h>
9 #include <linux/slab.h>
10 #include <linux/mxm-wmi.h>
11 #include <linux/vga_switcheroo.h>
12 #include <drm/drm_edid.h>
13 #include <acpi/video.h>
14
15 #include "nouveau_drv.h"
16 #include "nouveau_acpi.h"
17
18 #define NOUVEAU_DSM_LED 0x02
19 #define NOUVEAU_DSM_LED_STATE 0x00
20 #define NOUVEAU_DSM_LED_OFF 0x10
21 #define NOUVEAU_DSM_LED_STAMINA 0x11
22 #define NOUVEAU_DSM_LED_SPEED 0x12
23
24 #define NOUVEAU_DSM_POWER 0x03
25 #define NOUVEAU_DSM_POWER_STATE 0x00
26 #define NOUVEAU_DSM_POWER_SPEED 0x01
27 #define NOUVEAU_DSM_POWER_STAMINA 0x02
28
29 #define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
30 #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
31
32 #define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24)
33 #define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24)
34 #define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1)
35
36 #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
37
38 /* result of the optimus caps function */
39 #define OPTIMUS_ENABLED (1 << 0)
40 #define OPTIMUS_STATUS_MASK (3 << 3)
41 #define OPTIMUS_STATUS_OFF (0 << 3)
42 #define OPTIMUS_STATUS_ON_ENABLED (1 << 3)
43 #define OPTIMUS_STATUS_PWR_STABLE (3 << 3)
44 #define OPTIMUS_DISPLAY_HOTPLUG (1 << 6)
45 #define OPTIMUS_CAPS_MASK (7 << 24)
46 #define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24)
47
48 #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
49 #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
50
51 static struct nouveau_dsm_priv {
52 bool dsm_detected;
53 bool optimus_detected;
54 bool optimus_flags_detected;
55 bool optimus_skip_dsm;
56 acpi_handle dhandle;
57 acpi_handle rom_handle;
58 } nouveau_dsm_priv;
59
60 bool nouveau_is_optimus(void) {
61 return nouveau_dsm_priv.optimus_detected;
62 }
63
64 bool nouveau_is_v1_dsm(void) {
65 return nouveau_dsm_priv.dsm_detected;
66 }
67
68 #ifdef CONFIG_VGA_SWITCHEROO
69 static const guid_t nouveau_dsm_muid =
70 GUID_INIT(0x9D95A0A0, 0x0060, 0x4D48,
71 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4);
72
73 static const guid_t nouveau_op_dsm_muid =
74 GUID_INIT(0xA486D8F8, 0x0BDA, 0x471B,
75 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0);
76
77 static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
78 {
79 int i;
80 union acpi_object *obj;
81 char args_buff[4];
82 union acpi_object argv4 = {
83 .buffer.type = ACPI_TYPE_BUFFER,
84 .buffer.length = 4,
85 .buffer.pointer = args_buff
86 };
87
88 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
89 for (i = 0; i < 4; i++)
90 args_buff[i] = (arg >> i * 8) & 0xFF;
91
92 *result = 0;
93 obj = acpi_evaluate_dsm_typed(handle, &nouveau_op_dsm_muid, 0x00000100,
94 func, &argv4, ACPI_TYPE_BUFFER);
95 if (!obj) {
96 acpi_handle_info(handle, "failed to evaluate _DSM\n");
97 return AE_ERROR;
98 } else {
99 if (obj->buffer.length == 4) {
100 *result |= obj->buffer.pointer[0];
101 *result |= (obj->buffer.pointer[1] << 8);
102 *result |= (obj->buffer.pointer[2] << 16);
103 *result |= (obj->buffer.pointer[3] << 24);
104 }
105 ACPI_FREE(obj);
106 }
107
108 return 0;
109 }
110
111 /*
112 * On some platforms, _DSM(nouveau_op_dsm_muid, func0) has special
113 * requirements on the fourth parameter, so a private implementation
114 * instead of using acpi_check_dsm().
115 */
116 static int nouveau_dsm_get_optimus_functions(acpi_handle handle)
117 {
118 int result;
119
120 /*
121 * Function 0 returns a Buffer containing available functions.
122 * The args parameter is ignored for function 0, so just put 0 in it
123 */
124 if (nouveau_optimus_dsm(handle, 0, 0, &result))
125 return 0;
126
127 /*
128 * ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported.
129 * If the n-th bit is enabled, function n is supported
130 */
131 if (result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS))
132 return result;
133 return 0;
134 }
135
136 static int nouveau_dsm(acpi_handle handle, int func, int arg)
137 {
138 int ret = 0;
139 union acpi_object *obj;
140 union acpi_object argv4 = {
141 .integer.type = ACPI_TYPE_INTEGER,
142 .integer.value = arg,
143 };
144
145 obj = acpi_evaluate_dsm_typed(handle, &nouveau_dsm_muid, 0x00000102,
146 func, &argv4, ACPI_TYPE_INTEGER);
147 if (!obj) {
148 acpi_handle_info(handle, "failed to evaluate _DSM\n");
149 return AE_ERROR;
150 } else {
151 if (obj->integer.value == 0x80000002)
152 ret = -ENODEV;
153 ACPI_FREE(obj);
154 }
155
156 return ret;
157 }
158
159 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
160 {
161 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
162 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
163 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id);
164 }
165
166 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
167 {
168 int arg;
169 if (state == VGA_SWITCHEROO_ON)
170 arg = NOUVEAU_DSM_POWER_SPEED;
171 else
172 arg = NOUVEAU_DSM_POWER_STAMINA;
173 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg);
174 return 0;
175 }
176
177 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
178 {
179 if (!nouveau_dsm_priv.dsm_detected)
180 return 0;
181 if (id == VGA_SWITCHEROO_IGD)
182 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
183 else
184 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
185 }
186
187 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
188 enum vga_switcheroo_state state)
189 {
190 if (id == VGA_SWITCHEROO_IGD)
191 return 0;
192
193 /* Optimus laptops have the card already disabled in
194 * nouveau_switcheroo_set_state */
195 if (!nouveau_dsm_priv.dsm_detected)
196 return 0;
197
198 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
199 }
200
201 static enum vga_switcheroo_client_id nouveau_dsm_get_client_id(struct pci_dev *pdev)
202 {
203 /* easy option one - intel vendor ID means Integrated */
204 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
205 return VGA_SWITCHEROO_IGD;
206
207 /* is this device on Bus 0? - this may need improving */
208 if (pdev->bus->number == 0)
209 return VGA_SWITCHEROO_IGD;
210
211 return VGA_SWITCHEROO_DIS;
212 }
213
214 static const struct vga_switcheroo_handler nouveau_dsm_handler = {
215 .switchto = nouveau_dsm_switchto,
216 .power_state = nouveau_dsm_power_state,
217 .get_client_id = nouveau_dsm_get_client_id,
218 };
219
220 /*
221 * Firmware supporting Windows 8 or later do not use _DSM to put the device into
222 * D3cold, they instead rely on disabling power resources on the parent.
223 */
224 static bool nouveau_pr3_present(struct pci_dev *pdev)
225 {
226 struct pci_dev *parent_pdev = pci_upstream_bridge(pdev);
227 struct acpi_device *parent_adev;
228
229 if (!parent_pdev)
230 return false;
231
232 if (!parent_pdev->bridge_d3) {
233 /*
234 * Parent PCI bridge is currently not power managed.
235 * Since userspace can change these afterwards to be on
236 * the safe side we stick with _DSM and prevent usage of
237 * _PR3 from the bridge.
238 */
239 pci_d3cold_disable(pdev);
240 return false;
241 }
242
243 parent_adev = ACPI_COMPANION(&parent_pdev->dev);
244 if (!parent_adev)
245 return false;
246
247 return parent_adev->power.flags.power_resources &&
248 acpi_has_method(parent_adev->handle, "_PR3");
249 }
250
251 static void nouveau_dsm_pci_probe(struct pci_dev *pdev, acpi_handle *dhandle_out,
252 bool *has_mux, bool *has_opt,
253 bool *has_opt_flags, bool *has_pr3)
254 {
255 acpi_handle dhandle;
256 bool supports_mux;
257 int optimus_funcs;
258
259 dhandle = ACPI_HANDLE(&pdev->dev);
260 if (!dhandle)
261 return;
262
263 if (!acpi_has_method(dhandle, "_DSM"))
264 return;
265
266 supports_mux = acpi_check_dsm(dhandle, &nouveau_dsm_muid, 0x00000102,
267 1 << NOUVEAU_DSM_POWER);
268 optimus_funcs = nouveau_dsm_get_optimus_functions(dhandle);
269
270 /* Does not look like a Nvidia device. */
271 if (!supports_mux && !optimus_funcs)
272 return;
273
274 *dhandle_out = dhandle;
275 *has_mux = supports_mux;
276 *has_opt = !!optimus_funcs;
277 *has_opt_flags = optimus_funcs & (1 << NOUVEAU_DSM_OPTIMUS_FLAGS);
278 *has_pr3 = false;
279
280 if (optimus_funcs) {
281 uint32_t result;
282 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
283 &result);
284 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
285 (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
286 (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
287 (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
288
289 *has_pr3 = nouveau_pr3_present(pdev);
290 }
291 }
292
293 static bool nouveau_dsm_detect(void)
294 {
295 char acpi_method_name[255] = { 0 };
296 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
297 struct pci_dev *pdev = NULL;
298 acpi_handle dhandle = NULL;
299 bool has_mux = false;
300 bool has_optimus = false;
301 bool has_optimus_flags = false;
302 bool has_power_resources = false;
303 int vga_count = 0;
304 bool guid_valid;
305 bool ret = false;
306
307 /* lookup the MXM GUID */
308 guid_valid = mxm_wmi_supported();
309
310 if (guid_valid)
311 printk("MXM: GUID detected in BIOS\n");
312
313 /* now do DSM detection */
314 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
315 vga_count++;
316
317 nouveau_dsm_pci_probe(pdev, &dhandle, &has_mux, &has_optimus,
318 &has_optimus_flags, &has_power_resources);
319 }
320
321 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
322 vga_count++;
323
324 nouveau_dsm_pci_probe(pdev, &dhandle, &has_mux, &has_optimus,
325 &has_optimus_flags, &has_power_resources);
326 }
327
328 /* find the optimus DSM or the old v1 DSM */
329 if (has_optimus) {
330 nouveau_dsm_priv.dhandle = dhandle;
331 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332 &buffer);
333 pr_info("VGA switcheroo: detected Optimus DSM method %s handle\n",
334 acpi_method_name);
335 if (has_power_resources)
336 pr_info("nouveau: detected PR support, will not use DSM\n");
337 nouveau_dsm_priv.optimus_detected = true;
338 nouveau_dsm_priv.optimus_flags_detected = has_optimus_flags;
339 nouveau_dsm_priv.optimus_skip_dsm = has_power_resources;
340 ret = true;
341 } else if (vga_count == 2 && has_mux && guid_valid) {
342 nouveau_dsm_priv.dhandle = dhandle;
343 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
344 &buffer);
345 pr_info("VGA switcheroo: detected DSM switching method %s handle\n",
346 acpi_method_name);
347 nouveau_dsm_priv.dsm_detected = true;
348 ret = true;
349 }
350
351
352 return ret;
353 }
354
355 void nouveau_register_dsm_handler(void)
356 {
357 bool r;
358
359 r = nouveau_dsm_detect();
360 if (!r)
361 return;
362
363 vga_switcheroo_register_handler(&nouveau_dsm_handler, 0);
364 }
365
366 /* Must be called for Optimus models before the card can be turned off */
367 void nouveau_switcheroo_optimus_dsm(void)
368 {
369 u32 result = 0;
370 if (!nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.optimus_skip_dsm)
371 return;
372
373 if (nouveau_dsm_priv.optimus_flags_detected)
374 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
375 0x3, &result);
376
377 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
378 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
379
380 }
381
382 void nouveau_unregister_dsm_handler(void)
383 {
384 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
385 vga_switcheroo_unregister_handler();
386 }
387 #else
388 void nouveau_register_dsm_handler(void) {}
389 void nouveau_unregister_dsm_handler(void) {}
390 void nouveau_switcheroo_optimus_dsm(void) {}
391 #endif
392
393 /* retrieve the ROM in 4k blocks */
394 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
395 int offset, int len)
396 {
397 acpi_status status;
398 union acpi_object rom_arg_elements[2], *obj;
399 struct acpi_object_list rom_arg;
400 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
401
402 rom_arg.count = 2;
403 rom_arg.pointer = &rom_arg_elements[0];
404
405 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
406 rom_arg_elements[0].integer.value = offset;
407
408 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
409 rom_arg_elements[1].integer.value = len;
410
411 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
412 if (ACPI_FAILURE(status)) {
413 pr_info("failed to evaluate ROM got %s\n",
414 acpi_format_exception(status));
415 return -ENODEV;
416 }
417 obj = (union acpi_object *)buffer.pointer;
418 len = min(len, (int)obj->buffer.length);
419 memcpy(bios+offset, obj->buffer.pointer, len);
420 kfree(buffer.pointer);
421 return len;
422 }
423
424 bool nouveau_acpi_rom_supported(struct device *dev)
425 {
426 acpi_status status;
427 acpi_handle dhandle, rom_handle;
428
429 dhandle = ACPI_HANDLE(dev);
430 if (!dhandle)
431 return false;
432
433 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
434 if (ACPI_FAILURE(status))
435 return false;
436
437 nouveau_dsm_priv.rom_handle = rom_handle;
438 return true;
439 }
440
441 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
442 {
443 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
444 }
445
446 void *
447 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
448 {
449 struct acpi_device *acpidev;
450 acpi_handle handle;
451 int type, ret;
452 void *edid;
453
454 switch (connector->connector_type) {
455 case DRM_MODE_CONNECTOR_LVDS:
456 case DRM_MODE_CONNECTOR_eDP:
457 type = ACPI_VIDEO_DISPLAY_LCD;
458 break;
459 default:
460 return NULL;
461 }
462
463 handle = ACPI_HANDLE(&dev->pdev->dev);
464 if (!handle)
465 return NULL;
466
467 ret = acpi_bus_get_device(handle, &acpidev);
468 if (ret)
469 return NULL;
470
471 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
472 if (ret < 0)
473 return NULL;
474
475 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
476 }
477