blob: d9f6120e972e649ccc77be83abb9a53f9660ec9a [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001#include <linux/pci.h>
2#include <linux/acpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/slab.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +10004#include <acpi/acpi_drivers.h>
5#include <acpi/acpi_bus.h>
Ben Skeggsa6ed76d2010-07-12 15:33:07 +10006#include <acpi/video.h>
Dave Airlie81161882010-12-06 12:57:45 +10007#include <acpi/acpi.h>
8#include <linux/mxm-wmi.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +10009
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100010#include <linux/vga_switcheroo.h>
11
Linus Torvalds612a9aa2012-10-03 23:29:23 -070012#include <drm/drm_edid.h>
Ben Skeggsc0077062012-07-26 08:51:21 +100013
14#include "nouveau_drm.h"
15#include "nouveau_acpi.h"
16
Ben Skeggs6ee73862009-12-11 19:24:15 +100017#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
Dave Airlie5addcf02012-09-10 14:20:51 +100028#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 */
Peter Lekensteynd0992302011-12-17 12:54:04 +010049
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100050static struct nouveau_dsm_priv {
51 bool dsm_detected;
Dave Airlief19467c2011-03-22 14:10:27 +100052 bool optimus_detected;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100053 acpi_handle dhandle;
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +010054 acpi_handle other_handle;
Dave Airlieafeb3e12010-04-07 13:55:09 +100055 acpi_handle rom_handle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100056} nouveau_dsm_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +100057
Dave Airliec839d742012-11-02 11:04:27 +100058bool 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
Dave Airlief19467c2011-03-22 14:10:27 +100066#define NOUVEAU_DSM_HAS_MUX 0x1
67#define NOUVEAU_DSM_HAS_OPT 0x2
68
Jeff Mahoneyd0ce7b8562014-01-21 14:34:52 -080069#ifdef CONFIG_VGA_SWITCHEROO
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100070static const char nouveau_dsm_muid[] = {
71 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
72 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
73};
74
Dave Airlief19467c2011-03-22 14:10:27 +100075static const char nouveau_op_dsm_muid[] = {
76 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
77 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
78};
79
80static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
81{
82 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
83 struct acpi_object_list input;
84 union acpi_object params[4];
85 union acpi_object *obj;
Peter Lekensteynd0992302011-12-17 12:54:04 +010086 int i, err;
87 char args_buff[4];
Dave Airlief19467c2011-03-22 14:10:27 +100088
89 input.count = 4;
90 input.pointer = params;
91 params[0].type = ACPI_TYPE_BUFFER;
92 params[0].buffer.length = sizeof(nouveau_op_dsm_muid);
93 params[0].buffer.pointer = (char *)nouveau_op_dsm_muid;
94 params[1].type = ACPI_TYPE_INTEGER;
95 params[1].integer.value = 0x00000100;
96 params[2].type = ACPI_TYPE_INTEGER;
97 params[2].integer.value = func;
98 params[3].type = ACPI_TYPE_BUFFER;
Peter Lekensteynd0992302011-12-17 12:54:04 +010099 params[3].buffer.length = 4;
100 /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
101 for (i = 0; i < 4; i++)
102 args_buff[i] = (arg >> i * 8) & 0xFF;
103 params[3].buffer.pointer = args_buff;
Dave Airlief19467c2011-03-22 14:10:27 +1000104
105 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
106 if (err) {
107 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
108 return err;
109 }
110
111 obj = (union acpi_object *)output.pointer;
112
113 if (obj->type == ACPI_TYPE_INTEGER)
114 if (obj->integer.value == 0x80000002) {
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
Francisco Jerez6e86e042010-07-03 18:36:39 +0200132static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000133{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134 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
Ben Skeggs6ee73862009-12-11 19:24:15 +1000140 input.count = 4;
141 input.pointer = params;
142 params[0].type = ACPI_TYPE_BUFFER;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000143 params[0].buffer.length = sizeof(nouveau_dsm_muid);
144 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000145 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) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000154 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155 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 return -ENODEV;
163
164 if (obj->type == ACPI_TYPE_BUFFER) {
165 if (obj->buffer.length == 4 && result) {
166 *result = 0;
167 *result |= obj->buffer.pointer[0];
168 *result |= (obj->buffer.pointer[1] << 8);
169 *result |= (obj->buffer.pointer[2] << 16);
170 *result |= (obj->buffer.pointer[3] << 24);
171 }
172 }
173
174 kfree(output.pointer);
175 return 0;
176}
177
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100178/* Returns 1 if a DSM function is usable and 0 otherwise */
179static int nouveau_test_dsm(acpi_handle test_handle,
180 int (*dsm_func)(acpi_handle, int, int, uint32_t *),
181 int sfnc)
182{
183 u32 result = 0;
184
185 /* Function 0 returns a Buffer containing available functions. The args
186 * parameter is ignored for function 0, so just put 0 in it */
187 if (dsm_func(test_handle, 0, 0, &result))
188 return 0;
189
190 /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
191 * the n-th bit is enabled, function n is supported */
192 return result & 1 && result & (1 << sfnc);
193}
194
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000195static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196{
Dave Airlie000703f2011-05-09 11:40:25 +1000197 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
Dave Airlie81161882010-12-06 12:57:45 +1000198 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000199 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
200}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000202static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
203{
204 int arg;
205 if (state == VGA_SWITCHEROO_ON)
206 arg = NOUVEAU_DSM_POWER_SPEED;
207 else
208 arg = NOUVEAU_DSM_POWER_STAMINA;
209 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210 return 0;
211}
212
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000213static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214{
Dave Airliec839d742012-11-02 11:04:27 +1000215 if (!nouveau_dsm_priv.dsm_detected)
Peter Lekensteynd0992302011-12-17 12:54:04 +0100216 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000217 if (id == VGA_SWITCHEROO_IGD)
Dave Airliefc5ea292010-05-31 17:10:52 +1000218 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000219 else
Dave Airliefc5ea292010-05-31 17:10:52 +1000220 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000221}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000223static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
224 enum vga_switcheroo_state state)
225{
226 if (id == VGA_SWITCHEROO_IGD)
227 return 0;
228
Peter Lekensteynd0992302011-12-17 12:54:04 +0100229 /* Optimus laptops have the card already disabled in
230 * nouveau_switcheroo_set_state */
Dave Airliec839d742012-11-02 11:04:27 +1000231 if (!nouveau_dsm_priv.dsm_detected)
Peter Lekensteynd0992302011-12-17 12:54:04 +0100232 return 0;
233
Dave Airliefc5ea292010-05-31 17:10:52 +1000234 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000235}
236
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000237static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
238{
Dave Airlied1fbd9232010-12-06 12:56:44 +1000239 /* easy option one - intel vendor ID means Integrated */
240 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000241 return VGA_SWITCHEROO_IGD;
Dave Airlied1fbd9232010-12-06 12:56:44 +1000242
243 /* is this device on Bus 0? - this may need improving */
244 if (pdev->bus->number == 0)
245 return VGA_SWITCHEROO_IGD;
246
247 return VGA_SWITCHEROO_DIS;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000248}
249
250static struct vga_switcheroo_handler nouveau_dsm_handler = {
251 .switchto = nouveau_dsm_switchto,
252 .power_state = nouveau_dsm_power_state,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000253 .get_client_id = nouveau_dsm_get_client_id,
254};
255
Dave Airlief19467c2011-03-22 14:10:27 +1000256static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000257{
Zhang Rui187b5b52013-09-03 08:32:00 +0800258 acpi_handle dhandle;
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100259 int retval = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000260
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100261 dhandle = ACPI_HANDLE(&pdev->dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000262 if (!dhandle)
263 return false;
Dave Airlieafeb3e12010-04-07 13:55:09 +1000264
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100265 if (!acpi_has_method(dhandle, "_DSM")) {
266 nouveau_dsm_priv.other_handle = dhandle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000267 return false;
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100268 }
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100269 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
Dave Airlief19467c2011-03-22 14:10:27 +1000270 retval |= NOUVEAU_DSM_HAS_MUX;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000271
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100272 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
Dave Airlie5addcf02012-09-10 14:20:51 +1000273 NOUVEAU_DSM_OPTIMUS_CAPS))
Dave Airlief19467c2011-03-22 14:10:27 +1000274 retval |= NOUVEAU_DSM_HAS_OPT;
275
Dave Airlie5addcf02012-09-10 14:20:51 +1000276 if (retval & NOUVEAU_DSM_HAS_OPT) {
277 uint32_t result;
278 nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
279 &result);
280 dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
281 (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
282 (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
283 (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
284 }
Dave Airlief19467c2011-03-22 14:10:27 +1000285 if (retval)
286 nouveau_dsm_priv.dhandle = dhandle;
287
288 return retval;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000289}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000290
291static bool nouveau_dsm_detect(void)
292{
293 char acpi_method_name[255] = { 0 };
294 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
295 struct pci_dev *pdev = NULL;
296 int has_dsm = 0;
Dave Airlieaddde4e2012-05-02 20:26:24 +0100297 int has_optimus = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000298 int vga_count = 0;
Dave Airlie81161882010-12-06 12:57:45 +1000299 bool guid_valid;
Dave Airlief19467c2011-03-22 14:10:27 +1000300 int retval;
301 bool ret = false;
Dave Airlie81161882010-12-06 12:57:45 +1000302
Dave Airlief19467c2011-03-22 14:10:27 +1000303 /* lookup the MXM GUID */
Dave Airlie81161882010-12-06 12:57:45 +1000304 guid_valid = mxm_wmi_supported();
Dave Airlie81161882010-12-06 12:57:45 +1000305
Dave Airlief19467c2011-03-22 14:10:27 +1000306 if (guid_valid)
307 printk("MXM: GUID detected in BIOS\n");
Dave Airlieafeb3e12010-04-07 13:55:09 +1000308
Dave Airlief19467c2011-03-22 14:10:27 +1000309 /* now do DSM detection */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000310 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
311 vga_count++;
312
Dave Airlief19467c2011-03-22 14:10:27 +1000313 retval = nouveau_dsm_pci_probe(pdev);
Dave Airlief19467c2011-03-22 14:10:27 +1000314 if (retval & NOUVEAU_DSM_HAS_MUX)
315 has_dsm |= 1;
316 if (retval & NOUVEAU_DSM_HAS_OPT)
317 has_optimus = 1;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000318 }
319
Emil Velikov4c60fac2013-10-09 08:25:16 +0100320 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
321 vga_count++;
322
323 retval = nouveau_dsm_pci_probe(pdev);
324 if (retval & NOUVEAU_DSM_HAS_MUX)
325 has_dsm |= 1;
326 if (retval & NOUVEAU_DSM_HAS_OPT)
327 has_optimus = 1;
328 }
329
Dave Airliec839d742012-11-02 11:04:27 +1000330 /* find the optimus DSM or the old v1 DSM */
331 if (has_optimus == 1) {
332 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
333 &buffer);
334 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
335 acpi_method_name);
336 nouveau_dsm_priv.optimus_detected = true;
337 ret = true;
338 } else if (vga_count == 2 && has_dsm && guid_valid) {
Peter Lekensteynd0992302011-12-17 12:54:04 +0100339 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
340 &buffer);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000341 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
Peter Lekensteynd0992302011-12-17 12:54:04 +0100342 acpi_method_name);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000343 nouveau_dsm_priv.dsm_detected = true;
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100344 /*
345 * On some systems hotplug events are generated for the device
346 * being switched off when _DSM is executed. They cause ACPI
347 * hotplug to trigger and attempt to remove the device from
348 * the system, which causes it to break down. Prevent that from
349 * happening by setting the no_hotplug flag for the involved
350 * ACPI device objects.
351 */
352 acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
353 acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
Dave Airlief19467c2011-03-22 14:10:27 +1000354 ret = true;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000355 }
Dave Airlief19467c2011-03-22 14:10:27 +1000356
Dave Airlief19467c2011-03-22 14:10:27 +1000357
358 return ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000359}
360
361void nouveau_register_dsm_handler(void)
362{
363 bool r;
364
365 r = nouveau_dsm_detect();
366 if (!r)
367 return;
368
369 vga_switcheroo_register_handler(&nouveau_dsm_handler);
370}
371
Peter Lekensteynd0992302011-12-17 12:54:04 +0100372/* Must be called for Optimus models before the card can be turned off */
373void nouveau_switcheroo_optimus_dsm(void)
374{
375 u32 result = 0;
376 if (!nouveau_dsm_priv.optimus_detected)
377 return;
378
Dave Airlie5addcf02012-09-10 14:20:51 +1000379 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
380 0x3, &result);
381
382 nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
383 NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
384
Peter Lekensteynd0992302011-12-17 12:54:04 +0100385}
386
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000387void nouveau_unregister_dsm_handler(void)
388{
Andreas Heider2f3787a2012-05-21 00:14:50 +0100389 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
390 vga_switcheroo_unregister_handler();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000391}
Jeff Mahoneyd0ce7b8562014-01-21 14:34:52 -0800392#else
393void nouveau_register_dsm_handler(void) {}
394void nouveau_unregister_dsm_handler(void) {}
395void nouveau_switcheroo_optimus_dsm(void) {}
396#endif
Dave Airlieafeb3e12010-04-07 13:55:09 +1000397
398/* retrieve the ROM in 4k blocks */
399static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
400 int offset, int len)
401{
402 acpi_status status;
403 union acpi_object rom_arg_elements[2], *obj;
404 struct acpi_object_list rom_arg;
405 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
406
407 rom_arg.count = 2;
408 rom_arg.pointer = &rom_arg_elements[0];
409
410 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
411 rom_arg_elements[0].integer.value = offset;
412
413 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
414 rom_arg_elements[1].integer.value = len;
415
416 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
417 if (ACPI_FAILURE(status)) {
418 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
419 return -ENODEV;
420 }
421 obj = (union acpi_object *)buffer.pointer;
422 memcpy(bios+offset, obj->buffer.pointer, len);
423 kfree(buffer.pointer);
424 return len;
425}
426
427bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
428{
429 acpi_status status;
430 acpi_handle dhandle, rom_handle;
431
Dave Airlief19467c2011-03-22 14:10:27 +1000432 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
Dave Airlieafeb3e12010-04-07 13:55:09 +1000433 return false;
434
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100435 dhandle = ACPI_HANDLE(&pdev->dev);
Dave Airlieafeb3e12010-04-07 13:55:09 +1000436 if (!dhandle)
437 return false;
438
439 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
440 if (ACPI_FAILURE(status))
441 return false;
442
443 nouveau_dsm_priv.rom_handle = rom_handle;
444 return true;
445}
446
447int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
448{
449 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
450}
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000451
Ben Skeggsc0077062012-07-26 08:51:21 +1000452void *
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000453nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
454{
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000455 struct acpi_device *acpidev;
456 acpi_handle handle;
457 int type, ret;
458 void *edid;
459
460 switch (connector->connector_type) {
461 case DRM_MODE_CONNECTOR_LVDS:
462 case DRM_MODE_CONNECTOR_eDP:
463 type = ACPI_VIDEO_DISPLAY_LCD;
464 break;
465 default:
Ben Skeggsc0077062012-07-26 08:51:21 +1000466 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000467 }
468
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100469 handle = ACPI_HANDLE(&dev->pdev->dev);
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000470 if (!handle)
Ben Skeggsc0077062012-07-26 08:51:21 +1000471 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000472
473 ret = acpi_bus_get_device(handle, &acpidev);
474 if (ret)
Ben Skeggsc0077062012-07-26 08:51:21 +1000475 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000476
477 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
478 if (ret < 0)
Ben Skeggsc0077062012-07-26 08:51:21 +1000479 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000480
Ben Skeggsc0077062012-07-26 08:51:21 +1000481 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000482}