]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_acpi.c
Merge tag 'for-v3.13-fixes' of git://git.infradead.org/battery-2.6
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
CommitLineData
6ee73861
BS
1#include <linux/pci.h>
2#include <linux/acpi.h>
5a0e3ad6 3#include <linux/slab.h>
6ee73861
BS
4#include <acpi/acpi_drivers.h>
5#include <acpi/acpi_bus.h>
a6ed76d7 6#include <acpi/video.h>
8116188f
DA
7#include <acpi/acpi.h>
8#include <linux/mxm-wmi.h>
6ee73861 9
6a9ee8af
DA
10#include <linux/vga_switcheroo.h>
11
612a9aab 12#include <drm/drm_edid.h>
c0077061
BS
13
14#include "nouveau_drm.h"
15#include "nouveau_acpi.h"
16
6ee73861
BS
17#define NOUVEAU_DSM_LED 0x02
18#define NOUVEAU_DSM_LED_STATE 0x00
19#define NOUVEAU_DSM_LED_OFF 0x10
20#define NOUVEAU_DSM_LED_STAMINA 0x11
21#define NOUVEAU_DSM_LED_SPEED 0x12
22
23#define NOUVEAU_DSM_POWER 0x03
24#define NOUVEAU_DSM_POWER_STATE 0x00
25#define NOUVEAU_DSM_POWER_SPEED 0x01
26#define NOUVEAU_DSM_POWER_STAMINA 0x02
27
5addcf0a
DA
28#define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
29#define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
30
31#define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24)
32#define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24)
33#define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1)
34
35#define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
36
37/* result of the optimus caps function */
38#define OPTIMUS_ENABLED (1 << 0)
39#define OPTIMUS_STATUS_MASK (3 << 3)
40#define OPTIMUS_STATUS_OFF (0 << 3)
41#define OPTIMUS_STATUS_ON_ENABLED (1 << 3)
42#define OPTIMUS_STATUS_PWR_STABLE (3 << 3)
43#define OPTIMUS_DISPLAY_HOTPLUG (1 << 6)
44#define OPTIMUS_CAPS_MASK (7 << 24)
45#define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24)
46
47#define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
48#define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
d099230c 49
6a9ee8af
DA
50static struct nouveau_dsm_priv {
51 bool dsm_detected;
f19467c5 52 bool optimus_detected;
6a9ee8af 53 acpi_handle dhandle;
f244d8b6 54 acpi_handle other_handle;
afeb3e11 55 acpi_handle rom_handle;
6a9ee8af
DA
56} nouveau_dsm_priv;
57
c839d748
DA
58bool nouveau_is_optimus(void) {
59 return nouveau_dsm_priv.optimus_detected;
60}
61
62bool nouveau_is_v1_dsm(void) {
63 return nouveau_dsm_priv.dsm_detected;
64}
65
f19467c5
DA
66#define NOUVEAU_DSM_HAS_MUX 0x1
67#define NOUVEAU_DSM_HAS_OPT 0x2
68
6a9ee8af
DA
69static const char nouveau_dsm_muid[] = {
70 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
71 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
72};
6ee73861 73
f19467c5
DA
74static const char nouveau_op_dsm_muid[] = {
75 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
76 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
77};
78
79static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
80{
81 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
82 struct acpi_object_list input;
83 union acpi_object params[4];
84 union acpi_object *obj;
d099230c
PL
85 int i, err;
86 char args_buff[4];
f19467c5
DA
87
88 input.count = 4;
89 input.pointer = params;
90 params[0].type = ACPI_TYPE_BUFFER;
91 params[0].buffer.length = sizeof(nouveau_op_dsm_muid);
92 params[0].buffer.pointer = (char *)nouveau_op_dsm_muid;
93 params[1].type = ACPI_TYPE_INTEGER;
94 params[1].integer.value = 0x00000100;
95 params[2].type = ACPI_TYPE_INTEGER;
96 params[2].integer.value = func;
97 params[3].type = ACPI_TYPE_BUFFER;
d099230c
PL
98 params[3].buffer.length = 4;
99 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
100 for (i = 0; i < 4; i++)
101 args_buff[i] = (arg >> i * 8) & 0xFF;
102 params[3].buffer.pointer = args_buff;
f19467c5
DA
103
104 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
105 if (err) {
106 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
107 return err;
108 }
109
110 obj = (union acpi_object *)output.pointer;
111
112 if (obj->type == ACPI_TYPE_INTEGER)
113 if (obj->integer.value == 0x80000002) {
114 return -ENODEV;
115 }
116
117 if (obj->type == ACPI_TYPE_BUFFER) {
118 if (obj->buffer.length == 4 && result) {
119 *result = 0;
120 *result |= obj->buffer.pointer[0];
121 *result |= (obj->buffer.pointer[1] << 8);
122 *result |= (obj->buffer.pointer[2] << 16);
123 *result |= (obj->buffer.pointer[3] << 24);
124 }
125 }
126
127 kfree(output.pointer);
128 return 0;
129}
130
6e86e041 131static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
6a9ee8af 132{
6ee73861
BS
133 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
134 struct acpi_object_list input;
135 union acpi_object params[4];
136 union acpi_object *obj;
137 int err;
138
6ee73861
BS
139 input.count = 4;
140 input.pointer = params;
141 params[0].type = ACPI_TYPE_BUFFER;
6a9ee8af
DA
142 params[0].buffer.length = sizeof(nouveau_dsm_muid);
143 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
6ee73861
BS
144 params[1].type = ACPI_TYPE_INTEGER;
145 params[1].integer.value = 0x00000102;
146 params[2].type = ACPI_TYPE_INTEGER;
147 params[2].integer.value = func;
148 params[3].type = ACPI_TYPE_INTEGER;
149 params[3].integer.value = arg;
150
151 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
152 if (err) {
6a9ee8af 153 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
6ee73861
BS
154 return err;
155 }
156
157 obj = (union acpi_object *)output.pointer;
158
159 if (obj->type == ACPI_TYPE_INTEGER)
160 if (obj->integer.value == 0x80000002)
161 return -ENODEV;
162
163 if (obj->type == ACPI_TYPE_BUFFER) {
164 if (obj->buffer.length == 4 && result) {
165 *result = 0;
166 *result |= obj->buffer.pointer[0];
167 *result |= (obj->buffer.pointer[1] << 8);
168 *result |= (obj->buffer.pointer[2] << 16);
169 *result |= (obj->buffer.pointer[3] << 24);
170 }
171 }
172
173 kfree(output.pointer);
174 return 0;
175}
176
9075e85f
PL
177/* Returns 1 if a DSM function is usable and 0 otherwise */
178static int nouveau_test_dsm(acpi_handle test_handle,
179 int (*dsm_func)(acpi_handle, int, int, uint32_t *),
180 int sfnc)
181{
182 u32 result = 0;
183
184 /* Function 0 returns a Buffer containing available functions. The args
185 * parameter is ignored for function 0, so just put 0 in it */
186 if (dsm_func(test_handle, 0, 0, &result))
187 return 0;
188
189 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
190 * the n-th bit is enabled, function n is supported */
191 return result & 1 && result & (1 << sfnc);
192}
193
6a9ee8af 194static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
6ee73861 195{
000703f4 196 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
8116188f 197 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
6a9ee8af
DA
198 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
199}
200
201static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
202{
203 int arg;
204 if (state == VGA_SWITCHEROO_ON)
205 arg = NOUVEAU_DSM_POWER_SPEED;
206 else
207 arg = NOUVEAU_DSM_POWER_STAMINA;
208 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
209 return 0;
210}
211
212static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
213{
c839d748 214 if (!nouveau_dsm_priv.dsm_detected)
d099230c 215 return 0;
6a9ee8af 216 if (id == VGA_SWITCHEROO_IGD)
fc5ea29d 217 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
6a9ee8af 218 else
fc5ea29d 219 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
6a9ee8af 220}
6ee73861 221
6a9ee8af
DA
222static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
223 enum vga_switcheroo_state state)
224{
225 if (id == VGA_SWITCHEROO_IGD)
226 return 0;
227
d099230c
PL
228 /* Optimus laptops have the card already disabled in
229 * nouveau_switcheroo_set_state */
c839d748 230 if (!nouveau_dsm_priv.dsm_detected)
d099230c
PL
231 return 0;
232
fc5ea29d 233 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
6a9ee8af
DA
234}
235
6a9ee8af 236static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
6ee73861 237{
d1fbd923
DA
238 /* easy option one - intel vendor ID means Integrated */
239 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
6a9ee8af 240 return VGA_SWITCHEROO_IGD;
d1fbd923
DA
241
242 /* is this device on Bus 0? - this may need improving */
243 if (pdev->bus->number == 0)
244 return VGA_SWITCHEROO_IGD;
245
246 return VGA_SWITCHEROO_DIS;
6a9ee8af
DA
247}
248
249static struct vga_switcheroo_handler nouveau_dsm_handler = {
250 .switchto = nouveau_dsm_switchto,
251 .power_state = nouveau_dsm_power_state,
6a9ee8af
DA
252 .get_client_id = nouveau_dsm_get_client_id,
253};
6ee73861 254
f19467c5 255static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
6a9ee8af 256{
187b5b5d 257 acpi_handle dhandle;
9075e85f 258 int retval = 0;
6a9ee8af 259
3a83f992 260 dhandle = ACPI_HANDLE(&pdev->dev);
6a9ee8af
DA
261 if (!dhandle)
262 return false;
afeb3e11 263
f244d8b6
RW
264 if (!acpi_has_method(dhandle, "_DSM")) {
265 nouveau_dsm_priv.other_handle = dhandle;
6ee73861 266 return false;
f244d8b6 267 }
9075e85f 268 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
f19467c5
DA
269 retval |= NOUVEAU_DSM_HAS_MUX;
270
9075e85f 271 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
5addcf0a 272 NOUVEAU_DSM_OPTIMUS_CAPS))
f19467c5
DA
273 retval |= NOUVEAU_DSM_HAS_OPT;
274
5addcf0a
DA
275 if (retval & NOUVEAU_DSM_HAS_OPT) {
276 uint32_t result;
277 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
278 &result);
279 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
280 (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
281 (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
282 (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
283 }
f19467c5
DA
284 if (retval)
285 nouveau_dsm_priv.dhandle = dhandle;
6ee73861 286
f19467c5 287 return retval;
6ee73861 288}
6a9ee8af
DA
289
290static bool nouveau_dsm_detect(void)
291{
292 char acpi_method_name[255] = { 0 };
293 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
294 struct pci_dev *pdev = NULL;
295 int has_dsm = 0;
addde4ec 296 int has_optimus = 0;
6a9ee8af 297 int vga_count = 0;
8116188f 298 bool guid_valid;
f19467c5
DA
299 int retval;
300 bool ret = false;
8116188f 301
f19467c5 302 /* lookup the MXM GUID */
8116188f 303 guid_valid = mxm_wmi_supported();
8116188f 304
f19467c5
DA
305 if (guid_valid)
306 printk("MXM: GUID detected in BIOS\n");
afeb3e11 307
f19467c5 308 /* now do DSM detection */
6a9ee8af
DA
309 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
310 vga_count++;
311
f19467c5 312 retval = nouveau_dsm_pci_probe(pdev);
f19467c5
DA
313 if (retval & NOUVEAU_DSM_HAS_MUX)
314 has_dsm |= 1;
315 if (retval & NOUVEAU_DSM_HAS_OPT)
316 has_optimus = 1;
6a9ee8af
DA
317 }
318
4c60fac1
EV
319 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
320 vga_count++;
321
322 retval = nouveau_dsm_pci_probe(pdev);
323 if (retval & NOUVEAU_DSM_HAS_MUX)
324 has_dsm |= 1;
325 if (retval & NOUVEAU_DSM_HAS_OPT)
326 has_optimus = 1;
327 }
328
c839d748
DA
329 /* find the optimus DSM or the old v1 DSM */
330 if (has_optimus == 1) {
d099230c
PL
331 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332 &buffer);
c839d748 333 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
d099230c 334 acpi_method_name);
c839d748 335 nouveau_dsm_priv.optimus_detected = true;
f19467c5 336 ret = true;
c839d748 337 } else if (vga_count == 2 && has_dsm && guid_valid) {
d099230c
PL
338 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
339 &buffer);
c839d748 340 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
d099230c 341 acpi_method_name);
c839d748 342 nouveau_dsm_priv.dsm_detected = true;
f244d8b6
RW
343 /*
344 * On some systems hotplug events are generated for the device
345 * being switched off when _DSM is executed. They cause ACPI
346 * hotplug to trigger and attempt to remove the device from
347 * the system, which causes it to break down. Prevent that from
348 * happening by setting the no_hotplug flag for the involved
349 * ACPI device objects.
350 */
351 acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
352 acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
d099230c
PL
353 ret = true;
354 }
f19467c5 355
c839d748 356
f19467c5 357 return ret;
6a9ee8af
DA
358}
359
360void nouveau_register_dsm_handler(void)
361{
362 bool r;
363
364 r = nouveau_dsm_detect();
365 if (!r)
366 return;
367
368 vga_switcheroo_register_handler(&nouveau_dsm_handler);
369}
370
d099230c
PL
371/* Must be called for Optimus models before the card can be turned off */
372void nouveau_switcheroo_optimus_dsm(void)
373{
374 u32 result = 0;
375 if (!nouveau_dsm_priv.optimus_detected)
376 return;
377
5addcf0a
DA
378 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
379 0x3, &result);
380
381 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
382 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
383
d099230c
PL
384}
385
6a9ee8af
DA
386void nouveau_unregister_dsm_handler(void)
387{
2f3787aa
AH
388 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
389 vga_switcheroo_unregister_handler();
6a9ee8af 390}
afeb3e11
DA
391
392/* retrieve the ROM in 4k blocks */
393static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
394 int offset, int len)
395{
396 acpi_status status;
397 union acpi_object rom_arg_elements[2], *obj;
398 struct acpi_object_list rom_arg;
399 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
400
401 rom_arg.count = 2;
402 rom_arg.pointer = &rom_arg_elements[0];
403
404 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
405 rom_arg_elements[0].integer.value = offset;
406
407 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
408 rom_arg_elements[1].integer.value = len;
409
410 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
411 if (ACPI_FAILURE(status)) {
412 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
413 return -ENODEV;
414 }
415 obj = (union acpi_object *)buffer.pointer;
416 memcpy(bios+offset, obj->buffer.pointer, len);
417 kfree(buffer.pointer);
418 return len;
419}
420
421bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
422{
423 acpi_status status;
424 acpi_handle dhandle, rom_handle;
425
f19467c5 426 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
afeb3e11
DA
427 return false;
428
3a83f992 429 dhandle = ACPI_HANDLE(&pdev->dev);
afeb3e11
DA
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
441int 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}
a6ed76d7 445
c0077061 446void *
a6ed76d7
BS
447nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
448{
a6ed76d7
BS
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:
c0077061 460 return NULL;
a6ed76d7
BS
461 }
462
3a83f992 463 handle = ACPI_HANDLE(&dev->pdev->dev);
a6ed76d7 464 if (!handle)
c0077061 465 return NULL;
a6ed76d7
BS
466
467 ret = acpi_bus_get_device(handle, &acpidev);
468 if (ret)
c0077061 469 return NULL;
a6ed76d7
BS
470
471 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
472 if (ret < 0)
c0077061 473 return NULL;
a6ed76d7 474
c0077061 475 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
a6ed76d7 476}