blob: 1aa33d96d5d642bf96e7ced654d833f82e987d33 [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
10#include "drmP.h"
11#include "drm.h"
12#include "drm_sarea.h"
13#include "drm_crtc_helper.h"
14#include "nouveau_drv.h"
15#include "nouveau_drm.h"
16#include "nv50_display.h"
Ben Skeggsa6ed76d2010-07-12 15:33:07 +100017#include "nouveau_connector.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100018
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100019#include <linux/vga_switcheroo.h>
20
Ben Skeggs6ee73862009-12-11 19:24:15 +100021#define NOUVEAU_DSM_SUPPORTED 0x00
22#define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
23
24#define NOUVEAU_DSM_ACTIVE 0x01
25#define NOUVEAU_DSM_ACTIVE_QUERY 0x00
26
27#define NOUVEAU_DSM_LED 0x02
28#define NOUVEAU_DSM_LED_STATE 0x00
29#define NOUVEAU_DSM_LED_OFF 0x10
30#define NOUVEAU_DSM_LED_STAMINA 0x11
31#define NOUVEAU_DSM_LED_SPEED 0x12
32
33#define NOUVEAU_DSM_POWER 0x03
34#define NOUVEAU_DSM_POWER_STATE 0x00
35#define NOUVEAU_DSM_POWER_SPEED 0x01
36#define NOUVEAU_DSM_POWER_STAMINA 0x02
37
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038static struct nouveau_dsm_priv {
39 bool dsm_detected;
40 acpi_handle dhandle;
Dave Airlieafeb3e12010-04-07 13:55:09 +100041 acpi_handle rom_handle;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100042} nouveau_dsm_priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +100043
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100044static const char nouveau_dsm_muid[] = {
45 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
46 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
47};
48
Francisco Jerez6e86e042010-07-03 18:36:39 +020049static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100050{
Ben Skeggs6ee73862009-12-11 19:24:15 +100051 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
52 struct acpi_object_list input;
53 union acpi_object params[4];
54 union acpi_object *obj;
55 int err;
56
Ben Skeggs6ee73862009-12-11 19:24:15 +100057 input.count = 4;
58 input.pointer = params;
59 params[0].type = ACPI_TYPE_BUFFER;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100060 params[0].buffer.length = sizeof(nouveau_dsm_muid);
61 params[0].buffer.pointer = (char *)nouveau_dsm_muid;
Ben Skeggs6ee73862009-12-11 19:24:15 +100062 params[1].type = ACPI_TYPE_INTEGER;
63 params[1].integer.value = 0x00000102;
64 params[2].type = ACPI_TYPE_INTEGER;
65 params[2].integer.value = func;
66 params[3].type = ACPI_TYPE_INTEGER;
67 params[3].integer.value = arg;
68
69 err = acpi_evaluate_object(handle, "_DSM", &input, &output);
70 if (err) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100071 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
Ben Skeggs6ee73862009-12-11 19:24:15 +100072 return err;
73 }
74
75 obj = (union acpi_object *)output.pointer;
76
77 if (obj->type == ACPI_TYPE_INTEGER)
78 if (obj->integer.value == 0x80000002)
79 return -ENODEV;
80
81 if (obj->type == ACPI_TYPE_BUFFER) {
82 if (obj->buffer.length == 4 && result) {
83 *result = 0;
84 *result |= obj->buffer.pointer[0];
85 *result |= (obj->buffer.pointer[1] << 8);
86 *result |= (obj->buffer.pointer[2] << 16);
87 *result |= (obj->buffer.pointer[3] << 24);
88 }
89 }
90
91 kfree(output.pointer);
92 return 0;
93}
94
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100095static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
Ben Skeggs6ee73862009-12-11 19:24:15 +100096{
Dave Airlie81161882010-12-06 12:57:45 +100097 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 +100098 return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
99}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000100
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000101static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
102{
103 int arg;
104 if (state == VGA_SWITCHEROO_ON)
105 arg = NOUVEAU_DSM_POWER_SPEED;
106 else
107 arg = NOUVEAU_DSM_POWER_STAMINA;
108 nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000109 return 0;
110}
111
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000112static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000113{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000114 if (id == VGA_SWITCHEROO_IGD)
Dave Airliefc5ea292010-05-31 17:10:52 +1000115 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000116 else
Dave Airliefc5ea292010-05-31 17:10:52 +1000117 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000118}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000119
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000120static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
121 enum vga_switcheroo_state state)
122{
123 if (id == VGA_SWITCHEROO_IGD)
124 return 0;
125
Dave Airliefc5ea292010-05-31 17:10:52 +1000126 return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000127}
128
129static int nouveau_dsm_init(void)
130{
131 return 0;
132}
133
134static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
135{
Dave Airlied1fbd922010-12-06 12:56:44 +1000136 /* easy option one - intel vendor ID means Integrated */
137 if (pdev->vendor == PCI_VENDOR_ID_INTEL)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000138 return VGA_SWITCHEROO_IGD;
Dave Airlied1fbd922010-12-06 12:56:44 +1000139
140 /* is this device on Bus 0? - this may need improving */
141 if (pdev->bus->number == 0)
142 return VGA_SWITCHEROO_IGD;
143
144 return VGA_SWITCHEROO_DIS;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000145}
146
147static struct vga_switcheroo_handler nouveau_dsm_handler = {
148 .switchto = nouveau_dsm_switchto,
149 .power_state = nouveau_dsm_power_state,
150 .init = nouveau_dsm_init,
151 .get_client_id = nouveau_dsm_get_client_id,
152};
153
154static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
155{
156 acpi_handle dhandle, nvidia_handle;
157 acpi_status status;
158 int ret;
159 uint32_t result;
160
161 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
162 if (!dhandle)
163 return false;
Dave Airlieafeb3e12010-04-07 13:55:09 +1000164
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000165 status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
166 if (ACPI_FAILURE(status)) {
167 return false;
168 }
169
Dave Airliefc5ea292010-05-31 17:10:52 +1000170 ret = nouveau_dsm(dhandle, NOUVEAU_DSM_SUPPORTED,
171 NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000172 if (ret < 0)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000173 return false;
174
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000175 nouveau_dsm_priv.dhandle = dhandle;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176 return true;
177}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000178
179static bool nouveau_dsm_detect(void)
180{
181 char acpi_method_name[255] = { 0 };
182 struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
183 struct pci_dev *pdev = NULL;
184 int has_dsm = 0;
185 int vga_count = 0;
Dave Airlie81161882010-12-06 12:57:45 +1000186 bool guid_valid;
187
188 /* lookup the GUID */
189 guid_valid = mxm_wmi_supported();
190 if (!guid_valid)
191 return false;
192
193 printk("MXM GUID detected in BIOS\n");
Dave Airlieafeb3e12010-04-07 13:55:09 +1000194
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000195 while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
196 vga_count++;
197
198 has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
199 }
200
201 if (vga_count == 2 && has_dsm) {
Dave Airliefc5ea292010-05-31 17:10:52 +1000202 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000203 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
204 acpi_method_name);
205 nouveau_dsm_priv.dsm_detected = true;
206 return true;
207 }
208 return false;
209}
210
211void nouveau_register_dsm_handler(void)
212{
213 bool r;
214
215 r = nouveau_dsm_detect();
216 if (!r)
217 return;
218
219 vga_switcheroo_register_handler(&nouveau_dsm_handler);
220}
221
222void nouveau_unregister_dsm_handler(void)
223{
224 vga_switcheroo_unregister_handler();
225}
Dave Airlieafeb3e12010-04-07 13:55:09 +1000226
227/* retrieve the ROM in 4k blocks */
228static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
229 int offset, int len)
230{
231 acpi_status status;
232 union acpi_object rom_arg_elements[2], *obj;
233 struct acpi_object_list rom_arg;
234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
235
236 rom_arg.count = 2;
237 rom_arg.pointer = &rom_arg_elements[0];
238
239 rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
240 rom_arg_elements[0].integer.value = offset;
241
242 rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
243 rom_arg_elements[1].integer.value = len;
244
245 status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
246 if (ACPI_FAILURE(status)) {
247 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
248 return -ENODEV;
249 }
250 obj = (union acpi_object *)buffer.pointer;
251 memcpy(bios+offset, obj->buffer.pointer, len);
252 kfree(buffer.pointer);
253 return len;
254}
255
256bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
257{
258 acpi_status status;
259 acpi_handle dhandle, rom_handle;
260
261 if (!nouveau_dsm_priv.dsm_detected)
262 return false;
263
264 dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
265 if (!dhandle)
266 return false;
267
268 status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
269 if (ACPI_FAILURE(status))
270 return false;
271
272 nouveau_dsm_priv.rom_handle = rom_handle;
273 return true;
274}
275
276int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
277{
278 return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
279}
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000280
281int
282nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
283{
284 struct nouveau_connector *nv_connector = nouveau_connector(connector);
285 struct acpi_device *acpidev;
286 acpi_handle handle;
287 int type, ret;
288 void *edid;
289
290 switch (connector->connector_type) {
291 case DRM_MODE_CONNECTOR_LVDS:
292 case DRM_MODE_CONNECTOR_eDP:
293 type = ACPI_VIDEO_DISPLAY_LCD;
294 break;
295 default:
296 return -EINVAL;
297 }
298
299 handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
300 if (!handle)
301 return -ENODEV;
302
303 ret = acpi_bus_get_device(handle, &acpidev);
304 if (ret)
305 return -ENODEV;
306
307 ret = acpi_video_get_edid(acpidev, type, -1, &edid);
308 if (ret < 0)
309 return ret;
310
Ben Skeggs24b102d2010-09-10 15:33:11 +1000311 nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
Ben Skeggsa6ed76d2010-07-12 15:33:07 +1000312 return 0;
313}