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