Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[sfrench/cifs-2.6.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
7 #include "drmP.h"
8 #include "drm.h"
9 #include "drm_sarea.h"
10 #include "drm_crtc_helper.h"
11 #include "nouveau_drv.h"
12 #include "nouveau_drm.h"
13 #include "nv50_display.h"
14
15 #include <linux/vga_switcheroo.h>
16
17 #define NOUVEAU_DSM_SUPPORTED 0x00
18 #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
19
20 #define NOUVEAU_DSM_ACTIVE 0x01
21 #define NOUVEAU_DSM_ACTIVE_QUERY 0x00
22
23 #define NOUVEAU_DSM_LED 0x02
24 #define NOUVEAU_DSM_LED_STATE 0x00
25 #define NOUVEAU_DSM_LED_OFF 0x10
26 #define NOUVEAU_DSM_LED_STAMINA 0x11
27 #define NOUVEAU_DSM_LED_SPEED 0x12
28
29 #define NOUVEAU_DSM_POWER 0x03
30 #define NOUVEAU_DSM_POWER_STATE 0x00
31 #define NOUVEAU_DSM_POWER_SPEED 0x01
32 #define NOUVEAU_DSM_POWER_STAMINA 0x02
33
34 static struct nouveau_dsm_priv {
35         bool dsm_detected;
36         acpi_handle dhandle;
37         acpi_handle rom_handle;
38 } nouveau_dsm_priv;
39
40 static const char nouveau_dsm_muid[] = {
41         0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
42         0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
43 };
44
45 static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result)
46 {
47         struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
48         struct acpi_object_list input;
49         union acpi_object params[4];
50         union acpi_object *obj;
51         int err;
52
53         input.count = 4;
54         input.pointer = params;
55         params[0].type = ACPI_TYPE_BUFFER;
56         params[0].buffer.length = sizeof(nouveau_dsm_muid);
57         params[0].buffer.pointer = (char *)nouveau_dsm_muid;
58         params[1].type = ACPI_TYPE_INTEGER;
59         params[1].integer.value = 0x00000102;
60         params[2].type = ACPI_TYPE_INTEGER;
61         params[2].integer.value = func;
62         params[3].type = ACPI_TYPE_INTEGER;
63         params[3].integer.value = arg;
64
65         err = acpi_evaluate_object(handle, "_DSM", &input, &output);
66         if (err) {
67                 printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
68                 return err;
69         }
70
71         obj = (union acpi_object *)output.pointer;
72
73         if (obj->type == ACPI_TYPE_INTEGER)
74                 if (obj->integer.value == 0x80000002)
75                         return -ENODEV;
76
77         if (obj->type == ACPI_TYPE_BUFFER) {
78                 if (obj->buffer.length == 4 && result) {
79                         *result = 0;
80                         *result |= obj->buffer.pointer[0];
81                         *result |= (obj->buffer.pointer[1] << 8);
82                         *result |= (obj->buffer.pointer[2] << 16);
83                         *result |= (obj->buffer.pointer[3] << 24);
84                 }
85         }
86
87         kfree(output.pointer);
88         return 0;
89 }
90
91 static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
92 {
93         return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
94 }
95
96 static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
97 {
98         int arg;
99         if (state == VGA_SWITCHEROO_ON)
100                 arg = NOUVEAU_DSM_POWER_SPEED;
101         else
102                 arg = NOUVEAU_DSM_POWER_STAMINA;
103         nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
104         return 0;
105 }
106
107 static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
108 {
109         if (id == VGA_SWITCHEROO_IGD)
110                 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
111         else
112                 return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
113 }
114
115 static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
116                                    enum vga_switcheroo_state state)
117 {
118         if (id == VGA_SWITCHEROO_IGD)
119                 return 0;
120
121         return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
122 }
123
124 static int nouveau_dsm_init(void)
125 {
126         return 0;
127 }
128
129 static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
130 {
131         if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
132                 return VGA_SWITCHEROO_IGD;
133         else
134                 return VGA_SWITCHEROO_DIS;
135 }
136
137 static struct vga_switcheroo_handler nouveau_dsm_handler = {
138         .switchto = nouveau_dsm_switchto,
139         .power_state = nouveau_dsm_power_state,
140         .init = nouveau_dsm_init,
141         .get_client_id = nouveau_dsm_get_client_id,
142 };
143
144 static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
145 {
146         acpi_handle dhandle, nvidia_handle;
147         acpi_status status;
148         int ret;
149         uint32_t result;
150
151         dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
152         if (!dhandle)
153                 return false;
154
155         status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
156         if (ACPI_FAILURE(status)) {
157                 return false;
158         }
159
160         ret = nouveau_dsm(dhandle, NOUVEAU_DSM_SUPPORTED,
161                           NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
162         if (ret < 0)
163                 return false;
164
165         nouveau_dsm_priv.dhandle = dhandle;
166         return true;
167 }
168
169 static bool nouveau_dsm_detect(void)
170 {
171         char acpi_method_name[255] = { 0 };
172         struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
173         struct pci_dev *pdev = NULL;
174         int has_dsm = 0;
175         int vga_count = 0;
176
177         while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
178                 vga_count++;
179
180                 has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
181         }
182
183         if (vga_count == 2 && has_dsm) {
184                 acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer);
185                 printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
186                        acpi_method_name);
187                 nouveau_dsm_priv.dsm_detected = true;
188                 return true;
189         }
190         return false;
191 }
192
193 void nouveau_register_dsm_handler(void)
194 {
195         bool r;
196
197         r = nouveau_dsm_detect();
198         if (!r)
199                 return;
200
201         vga_switcheroo_register_handler(&nouveau_dsm_handler);
202 }
203
204 void nouveau_unregister_dsm_handler(void)
205 {
206         vga_switcheroo_unregister_handler();
207 }
208
209 /* retrieve the ROM in 4k blocks */
210 static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
211                             int offset, int len)
212 {
213         acpi_status status;
214         union acpi_object rom_arg_elements[2], *obj;
215         struct acpi_object_list rom_arg;
216         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
217
218         rom_arg.count = 2;
219         rom_arg.pointer = &rom_arg_elements[0];
220
221         rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
222         rom_arg_elements[0].integer.value = offset;
223
224         rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
225         rom_arg_elements[1].integer.value = len;
226
227         status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
228         if (ACPI_FAILURE(status)) {
229                 printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
230                 return -ENODEV;
231         }
232         obj = (union acpi_object *)buffer.pointer;
233         memcpy(bios+offset, obj->buffer.pointer, len);
234         kfree(buffer.pointer);
235         return len;
236 }
237
238 bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
239 {
240         acpi_status status;
241         acpi_handle dhandle, rom_handle;
242
243         if (!nouveau_dsm_priv.dsm_detected)
244                 return false;
245
246         dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
247         if (!dhandle)
248                 return false;
249
250         status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
251         if (ACPI_FAILURE(status))
252                 return false;
253
254         nouveau_dsm_priv.rom_handle = rom_handle;
255         return true;
256 }
257
258 int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
259 {
260         return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
261 }