blob: ba0183fb84f3b871a4815552021174e870f780e8 [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
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100069static 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
Dave Airlief19467c2011-03-22 14:10:27 +100074static 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;
Peter Lekensteynd0992302011-12-17 12:54:04 +010085 int i, err;
86 char args_buff[4];
Dave Airlief19467c2011-03-22 14:10:27 +100087
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;
Peter Lekensteynd0992302011-12-17 12:54:04 +010098 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;
Dave Airlief19467c2011-03-22 14:10:27 +1000103
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
Francisco Jerez6e86e042010-07-03 18:36:39 +0200131static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000132{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000133 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
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139 input.count = 4;
140 input.pointer = params;
141 params[0].type = ACPI_TYPE_BUFFER;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000142 params[0].buffer.length = sizeof(nouveau_dsm_muid);
143 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000144 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) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000153 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154 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
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100177/* 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
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000194static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195{
Dave Airlie000703f2011-05-09 11:40:25 +1000196 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 +1000197 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 +1000198 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
199}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000201static 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);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 return 0;
210}
211
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000212static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213{
Dave Airliec839d742012-11-02 11:04:27 +1000214 if (!nouveau_dsm_priv.dsm_detected)
Peter Lekensteynd0992302011-12-17 12:54:04 +0100215 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000216 if (id == VGA_SWITCHEROO_IGD)
Dave Airliefc5ea292010-05-31 17:10:52 +1000217 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000218 else
Dave Airliefc5ea292010-05-31 17:10:52 +1000219 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000220}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000221
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000222static 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
Peter Lekensteynd0992302011-12-17 12:54:04 +0100228 /* Optimus laptops have the card already disabled in
229 * nouveau_switcheroo_set_state */
Dave Airliec839d742012-11-02 11:04:27 +1000230 if (!nouveau_dsm_priv.dsm_detected)
Peter Lekensteynd0992302011-12-17 12:54:04 +0100231 return 0;
232
Dave Airliefc5ea292010-05-31 17:10:52 +1000233 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000234}
235
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000236static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
237{
Dave Airlied1fbd9232010-12-06 12:56:44 +1000238 /* easy option one - intel vendor ID means Integrated */
239 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000240 return VGA_SWITCHEROO_IGD;
Dave Airlied1fbd9232010-12-06 12:56:44 +1000241
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;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000247}
248
249static struct vga_switcheroo_handler nouveau_dsm_handler = {
250 .switchto = nouveau_dsm_switchto,
251 .power_state = nouveau_dsm_power_state,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000252 .get_client_id = nouveau_dsm_get_client_id,
253};
254
Dave Airlief19467c2011-03-22 14:10:27 +1000255static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000256{
Zhang Rui187b5b52013-09-03 08:32:00 +0800257 acpi_handle dhandle;
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100258 int retval = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000259
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100260 dhandle = ACPI_HANDLE(&pdev->dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000261 if (!dhandle)
262 return false;
Dave Airlieafeb3e12010-04-07 13:55:09 +1000263
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100264 if (!acpi_has_method(dhandle, "_DSM")) {
265 nouveau_dsm_priv.other_handle = dhandle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000266 return false;
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100267 }
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100268 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
Dave Airlief19467c2011-03-22 14:10:27 +1000269 retval |= NOUVEAU_DSM_HAS_MUX;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000270
Peter Lekensteyn9075e852011-12-17 12:53:43 +0100271 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
Dave Airlie5addcf02012-09-10 14:20:51 +1000272 NOUVEAU_DSM_OPTIMUS_CAPS))
Dave Airlief19467c2011-03-22 14:10:27 +1000273 retval |= NOUVEAU_DSM_HAS_OPT;
274
Dave Airlie5addcf02012-09-10 14:20:51 +1000275 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 }
Dave Airlief19467c2011-03-22 14:10:27 +1000284 if (retval)
285 nouveau_dsm_priv.dhandle = dhandle;
286
287 return retval;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000288}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000289
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;
Dave Airlieaddde4e2012-05-02 20:26:24 +0100296 int has_optimus = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000297 int vga_count = 0;
Dave Airlie81161882010-12-06 12:57:45 +1000298 bool guid_valid;
Dave Airlief19467c2011-03-22 14:10:27 +1000299 int retval;
300 bool ret = false;
Dave Airlie81161882010-12-06 12:57:45 +1000301
Dave Airlief19467c2011-03-22 14:10:27 +1000302 /* lookup the MXM GUID */
Dave Airlie81161882010-12-06 12:57:45 +1000303 guid_valid = mxm_wmi_supported();
Dave Airlie81161882010-12-06 12:57:45 +1000304
Dave Airlief19467c2011-03-22 14:10:27 +1000305 if (guid_valid)
306 printk("MXM: GUID detected in BIOS\n");
Dave Airlieafeb3e12010-04-07 13:55:09 +1000307
Dave Airlief19467c2011-03-22 14:10:27 +1000308 /* now do DSM detection */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000309 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
310 vga_count++;
311
Dave Airlief19467c2011-03-22 14:10:27 +1000312 retval = nouveau_dsm_pci_probe(pdev);
Dave Airlief19467c2011-03-22 14:10:27 +1000313 if (retval & NOUVEAU_DSM_HAS_MUX)
314 has_dsm |= 1;
315 if (retval & NOUVEAU_DSM_HAS_OPT)
316 has_optimus = 1;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000317 }
318
Emil Velikov4c60fac2013-10-09 08:25:16 +0100319 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
Dave Airliec839d742012-11-02 11:04:27 +1000329 /* find the optimus DSM or the old v1 DSM */
330 if (has_optimus == 1) {
331 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
332 &buffer);
333 printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
334 acpi_method_name);
335 nouveau_dsm_priv.optimus_detected = true;
336 ret = true;
337 } else if (vga_count == 2 && has_dsm && guid_valid) {
Peter Lekensteynd0992302011-12-17 12:54:04 +0100338 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
339 &buffer);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000340 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
Peter Lekensteynd0992302011-12-17 12:54:04 +0100341 acpi_method_name);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000342 nouveau_dsm_priv.dsm_detected = true;
Rafael J. Wysockif244d8b2013-12-31 13:39:42 +0100343 /*
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);
Dave Airlief19467c2011-03-22 14:10:27 +1000353 ret = true;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000354 }
Dave Airlief19467c2011-03-22 14:10:27 +1000355
Dave Airlief19467c2011-03-22 14:10:27 +1000356
357 return ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000358}
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
Peter Lekensteynd0992302011-12-17 12:54:04 +0100371/* 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
Dave Airlie5addcf02012-09-10 14:20:51 +1000378 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
Peter Lekensteynd0992302011-12-17 12:54:04 +0100384}
385
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000386void nouveau_unregister_dsm_handler(void)
387{
Andreas Heider2f3787a2012-05-21 00:14:50 +0100388 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
389 vga_switcheroo_unregister_handler();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000390}
Dave Airlieafeb3e12010-04-07 13:55:09 +1000391
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
Dave Airlief19467c2011-03-22 14:10:27 +1000426 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
Dave Airlieafeb3e12010-04-07 13:55:09 +1000427 return false;
428
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100429 dhandle = ACPI_HANDLE(&pdev->dev);
Dave Airlieafeb3e12010-04-07 13:55:09 +1000430 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}
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000445
Ben Skeggsc0077062012-07-26 08:51:21 +1000446void *
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000447nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
448{
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000449 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:
Ben Skeggsc0077062012-07-26 08:51:21 +1000460 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000461 }
462
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100463 handle = ACPI_HANDLE(&dev->pdev->dev);
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000464 if (!handle)
Ben Skeggsc0077062012-07-26 08:51:21 +1000465 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000466
467 ret = acpi_bus_get_device(handle, &acpidev);
468 if (ret)
Ben Skeggsc0077062012-07-26 08:51:21 +1000469 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000470
471 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
472 if (ret < 0)
Ben Skeggsc0077062012-07-26 08:51:21 +1000473 return NULL;
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000474
Ben Skeggsc0077062012-07-26 08:51:21 +1000475 return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000476}