nouveau / ACPI: fix memory leak in ACPI _DSM related code
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_acpi.c
1 #include <linux/pci.h>
2 #include <linux/acpi.h>
3 #include <linux/slab.h>
4 #include <acpi/acpi_drivers.h>
5 #include <acpi/acpi_bus.h>
6 #include <acpi/video.h>
7 #include <acpi/acpi.h>
8 #include <linux/mxm-wmi.h>
9
10 #include <linux/vga_switcheroo.h>
11
12 #include <drm/drm_edid.h>
13
14 #include "nouveau_drm.h"
15 #include "nouveau_acpi.h"
16
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
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 */
49
50 static struct nouveau_dsm_priv {
51 bool dsm_detected;
52 bool optimus_detected;
53 acpi_handle dhandle;
54 acpi_handle other_handle;
55 acpi_handle rom_handle;
56 } nouveau_dsm_priv;
57
58 bool nouveau_is_optimus(void) {
59 return nouveau_dsm_priv.optimus_detected;
60 }
61
62 bool nouveau_is_v1_dsm(void) {
63 return nouveau_dsm_priv.dsm_detected;
64 }
65
66 #define NOUVEAU_DSM_HAS_MUX 0x1
67 #define NOUVEAU_DSM_HAS_OPT 0x2
68
69 static const char nouveau_dsm_muid[] = {
70 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
71 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
72 };
73
74 static 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
79 static 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;
85 int i, err;
86 char args_buff[4];
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;
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;
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 kfree(output.pointer);
115 return -ENODEV;
116 }
117
118 if (obj->type == ACPI_TYPE_BUFFER) {
119 if (obj->buffer.length == 4 && result) {
120 *result = 0;
121 *result |= obj->buffer.pointer[0];
122 *result |= (obj->buffer.pointer[1] << 8);
123 *result |= (obj->buffer.pointer[2] << 16);
124 *result |= (obj->buffer.pointer[3] << 24);
125 }
126 }
127
128 kfree(output.pointer);
129 return 0;
130 }
131
132 static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
133 {
134 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
135 struct acpi_object_list input;
136 union acpi_object params[4];
137 union acpi_object *obj;
138 int err;
139
140 input.count = 4;
141 input.pointer = params;
142 params[0].type = ACPI_TYPE_BUFFER;
143 params[0].buffer.length = sizeof(nouveau_dsm_muid);
144 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
145 params[1].type = ACPI_TYPE_INTEGER;
146 params[1].integer.value = 0x00000102;
147 params[2].type = ACPI_TYPE_INTEGER;
148 params[2].integer.value = func;
149 params[3].type = ACPI_TYPE_INTEGER;
150 params[3].integer.value = arg;
151
152 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
153 if (err) {
154 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
155 return err;
156 }
157
158 obj = (union acpi_object *)output.pointer;
159
160 if (obj->type == ACPI_TYPE_INTEGER)
161 if (obj->integer.value == 0x80000002) {
162 kfree(output.pointer);
163 return -ENODEV;
164 }
165
166 if (obj->type == ACPI_TYPE_BUFFER) {
167 if (obj->buffer.length == 4 && result) {
168 *result = 0;
169 *result |= obj->buffer.pointer[0];
170 *result |= (obj->buffer.pointer[1] << 8);
171 *result |= (obj->buffer.pointer[2] << 16);
172 *result |= (obj->buffer.pointer[3] << 24);
173 }
174 }
175
176 kfree(output.pointer);
177 return 0;
178 }
179
180 /* Returns 1 if a DSM function is usable and 0 otherwise */
181 static int nouveau_test_dsm(acpi_handle test_handle,
182 int (*dsm_func)(acpi_handle, int, int, uint32_t *),
183 int sfnc)
184 {
185 u32 result = 0;
186
187 /* Function 0 returns a Buffer containing available functions. The args
188 * parameter is ignored for function 0, so just put 0 in it */
189 if (dsm_func(test_handle, 0, 0, &result))
190 return 0;
191
192 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
193 * the n-th bit is enabled, function n is supported */
194 return result & 1 && result & (1 << sfnc);
195 }
196
197 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
198 {
199 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
200 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
201 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
202 }
203
204 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
205 {
206 int arg;
207 if (state == VGA_SWITCHEROO_ON)
208 arg = NOUVEAU_DSM_POWER_SPEED;
209 else
210 arg = NOUVEAU_DSM_POWER_STAMINA;
211 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
212 return 0;
213 }
214
215 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
216 {
217 if (!nouveau_dsm_priv.dsm_detected)
218 return 0;
219 if (id == VGA_SWITCHEROO_IGD)
220 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
221 else
222 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
223 }
224
225 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
226 enum vga_switcheroo_state state)
227 {
228 if (id == VGA_SWITCHEROO_IGD)
229 return 0;
230
231 /* Optimus laptops have the card already disabled in
232 * nouveau_switcheroo_set_state */
233 if (!nouveau_dsm_priv.dsm_detected)
234 return 0;
235
236 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
237 }
238
239 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
240 {
241 /* easy option one - intel vendor ID means Integrated */
242 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
243 return VGA_SWITCHEROO_IGD;
244
245 /* is this device on Bus 0? - this may need improving */
246 if (pdev->bus->number == 0)
247 return VGA_SWITCHEROO_IGD;
248
249 return VGA_SWITCHEROO_DIS;
250 }
251
252 static struct vga_switcheroo_handler nouveau_dsm_handler = {
253 .switchto = nouveau_dsm_switchto,
254 .power_state = nouveau_dsm_power_state,
255 .get_client_id = nouveau_dsm_get_client_id,
256 };
257
258 static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
259 {
260 acpi_handle dhandle;
261 int retval = 0;
262
263 dhandle = ACPI_HANDLE(&pdev->dev);
264 if (!dhandle)
265 return false;
266
267 if (!acpi_has_method(dhandle, "_DSM")) {
268 nouveau_dsm_priv.other_handle = dhandle;
269 return false;
270 }
271 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
272 retval |= NOUVEAU_DSM_HAS_MUX;
273
274 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
275 NOUVEAU_DSM_OPTIMUS_CAPS))
276 retval |= NOUVEAU_DSM_HAS_OPT;
277
278 if (retval & NOUVEAU_DSM_HAS_OPT) {
279 uint32_t result;
280 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
281 &result);
282 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
283 (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
284 (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
285 (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
286 }
287 if (retval)
288 nouveau_dsm_priv.dhandle = dhandle;
289
290 return retval;
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 int has_dsm = 0;
299 int has_optimus = 0;
300 int vga_count = 0;
301 bool guid_valid;
302 int retval;
303 bool ret = false;
304
305 /* lookup the MXM GUID */
306 guid_valid = mxm_wmi_supported();
307
308 if (guid_valid)
309 printk("MXM: GUID detected in BIOS\n");
310
311 /* now do DSM detection */
312 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
313 vga_count++;
314
315 retval = nouveau_dsm_pci_probe(pdev);
316 if (retval & NOUVEAU_DSM_HAS_MUX)
317 has_dsm |= 1;
318 if (retval & NOUVEAU_DSM_HAS_OPT)
319 has_optimus = 1;
320 }
321
322 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
323 vga_count++;
324
325 retval = nouveau_dsm_pci_probe(pdev);
326 if (retval & NOUVEAU_DSM_HAS_MUX)
327 has_dsm |= 1;
328 if (retval & NOUVEAU_DSM_HAS_OPT)
329 has_optimus = 1;
330 }
331
332 /* find the optimus DSM or the old v1 DSM */
333 if (has_optimus == 1) {
334 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
335 &buffer);
336 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
337 acpi_method_name);
338 nouveau_dsm_priv.optimus_detected = true;
339 ret = true;
340 } else if (vga_count == 2 && has_dsm && guid_valid) {
341 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
342 &buffer);
343 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
344 acpi_method_name);
345 nouveau_dsm_priv.dsm_detected = true;
346 /*
347 * On some systems hotplug events are generated for the device
348 * being switched off when _DSM is executed. They cause ACPI
349 * hotplug to trigger and attempt to remove the device from
350 * the system, which causes it to break down. Prevent that from
351 * happening by setting the no_hotplug flag for the involved
352 * ACPI device objects.
353 */
354 acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
355 acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
356 ret = true;
357 }
358
359
360 return ret;
361 }
362
363 void nouveau_register_dsm_handler(void)
364 {
365 bool r;
366
367 r = nouveau_dsm_detect();
368 if (!r)
369 return;
370
371 vga_switcheroo_register_handler(&nouveau_dsm_handler);
372 }
373
374 /* Must be called for Optimus models before the card can be turned off */
375 void nouveau_switcheroo_optimus_dsm(void)
376 {
377 u32 result = 0;
378 if (!nouveau_dsm_priv.optimus_detected)
379 return;
380
381 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
382 0x3, &result);
383
384 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
385 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
386
387 }
388
389 void nouveau_unregister_dsm_handler(void)
390 {
391 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
392 vga_switcheroo_unregister_handler();
393 }
394
395 /* retrieve the ROM in 4k blocks */
396 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
397 int offset, int len)
398 {
399 acpi_status status;
400 union acpi_object rom_arg_elements[2], *obj;
401 struct acpi_object_list rom_arg;
402 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
403
404 rom_arg.count = 2;
405 rom_arg.pointer = &rom_arg_elements[0];
406
407 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
408 rom_arg_elements[0].integer.value = offset;
409
410 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
411 rom_arg_elements[1].integer.value = len;
412
413 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
414 if (ACPI_FAILURE(status)) {
415 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
416 return -ENODEV;
417 }
418 obj = (union acpi_object *)buffer.pointer;
419 memcpy(bios+offset, obj->buffer.pointer, len);
420 kfree(buffer.pointer);
421 return len;
422 }
423
424 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
425 {
426 acpi_status status;
427 acpi_handle dhandle, rom_handle;
428
429 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
430 return false;
431
432 dhandle = ACPI_HANDLE(&pdev->dev);
433 if (!dhandle)
434 return false;
435
436 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
437 if (ACPI_FAILURE(status))
438 return false;
439
440 nouveau_dsm_priv.rom_handle = rom_handle;
441 return true;
442 }
443
444 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
445 {
446 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
447 }
448
449 void *
450 nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
451 {
452 struct acpi_device *acpidev;
453 acpi_handle handle;
454 int type, ret;
455 void *edid;
456
457 switch (connector->connector_type) {
458 case DRM_MODE_CONNECTOR_LVDS:
459 case DRM_MODE_CONNECTOR_eDP:
460 type = ACPI_VIDEO_DISPLAY_LCD;
461 break;
462 default:
463 return NULL;
464 }
465
466 handle = ACPI_HANDLE(&dev->pdev->dev);
467 if (!handle)
468 return NULL;
469
470 ret = acpi_bus_get_device(handle, &acpidev);
471 if (ret)
472 return NULL;
473
474 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
475 if (ret < 0)
476 return NULL;
477
478 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
479 }
This page took 0.043634 seconds and 5 git commands to generate.