]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nouveau_acpi.c
UAPI: (Scripted) Convert #include "..." to #include <path/...> in drivers/gpu/
[mirror_ubuntu-bionic-kernel.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 <drm/drmP.h>
11 #include <drm/drm_crtc_helper.h>
12 #include "nouveau_drv.h"
13 #include <drm/nouveau_drm.h>
14 #include "nv50_display.h"
15 #include "nouveau_connector.h"
16
17 #include <linux/vga_switcheroo.h>
18
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
30 #define NOUVEAU_DSM_OPTIMUS_FN 0x1A
31 #define NOUVEAU_DSM_OPTIMUS_ARGS 0x03000001
32
33 static struct nouveau_dsm_priv {
34 bool dsm_detected;
35 bool optimus_detected;
36 acpi_handle dhandle;
37 acpi_handle rom_handle;
38 } nouveau_dsm_priv;
39
40 #define NOUVEAU_DSM_HAS_MUX 0x1
41 #define NOUVEAU_DSM_HAS_OPT 0x2
42
43 static const char nouveau_dsm_muid[] = {
44 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
45 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
46 };
47
48 static 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
53 static 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;
59 int i, err;
60 char args_buff[4];
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;
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;
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
105 static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
106 {
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
113 input.count = 4;
114 input.pointer = params;
115 params[0].type = ACPI_TYPE_BUFFER;
116 params[0].buffer.length = sizeof(nouveau_dsm_muid);
117 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
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) {
127 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
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
151 /* Returns 1 if a DSM function is usable and 0 otherwise */
152 static 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
168 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
169 {
170 mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
171 mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
172 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
173 }
174
175 static 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
186 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
187 {
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;
192 if (id == VGA_SWITCHEROO_IGD)
193 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
194 else
195 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
196 }
197
198 static 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
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
209 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
210 }
211
212 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
213 {
214 /* easy option one - intel vendor ID means Integrated */
215 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
216 return VGA_SWITCHEROO_IGD;
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;
223 }
224
225 static struct vga_switcheroo_handler nouveau_dsm_handler = {
226 .switchto = nouveau_dsm_switchto,
227 .power_state = nouveau_dsm_power_state,
228 .get_client_id = nouveau_dsm_get_client_id,
229 };
230
231 static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
232 {
233 acpi_handle dhandle, nvidia_handle;
234 acpi_status status;
235 int retval = 0;
236
237 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
238 if (!dhandle)
239 return false;
240
241 status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
242 if (ACPI_FAILURE(status)) {
243 return false;
244 }
245
246 if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
247 retval |= NOUVEAU_DSM_HAS_MUX;
248
249 if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
250 NOUVEAU_DSM_OPTIMUS_FN))
251 retval |= NOUVEAU_DSM_HAS_OPT;
252
253 if (retval)
254 nouveau_dsm_priv.dhandle = dhandle;
255
256 return retval;
257 }
258
259 static 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;
265 int has_optimus = 0;
266 int vga_count = 0;
267 bool guid_valid;
268 int retval;
269 bool ret = false;
270
271 /* lookup the MXM GUID */
272 guid_valid = mxm_wmi_supported();
273
274 if (guid_valid)
275 printk("MXM: GUID detected in BIOS\n");
276
277 /* now do DSM detection */
278 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
279 vga_count++;
280
281 retval = nouveau_dsm_pci_probe(pdev);
282 if (retval & NOUVEAU_DSM_HAS_MUX)
283 has_dsm |= 1;
284 if (retval & NOUVEAU_DSM_HAS_OPT)
285 has_optimus = 1;
286 }
287
288 if (vga_count == 2 && has_dsm && guid_valid) {
289 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
290 &buffer);
291 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
292 acpi_method_name);
293 nouveau_dsm_priv.dsm_detected = true;
294 ret = true;
295 }
296
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);
302 nouveau_dsm_priv.optimus_detected = true;
303 ret = true;
304 }
305
306 return ret;
307 }
308
309 void 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
320 /* Must be called for Optimus models before the card can be turned off */
321 void 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
331 void nouveau_unregister_dsm_handler(void)
332 {
333 if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
334 vga_switcheroo_unregister_handler();
335 }
336
337 /* retrieve the ROM in 4k blocks */
338 static 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
366 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
367 {
368 acpi_status status;
369 acpi_handle dhandle, rom_handle;
370
371 if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
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
386 int 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 }
390
391 int
392 nouveau_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
421 nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
422 return 0;
423 }