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