Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / gpu / drm / nouveau / core / subdev / mxm / base.c
1 /*
2  * Copyright 2011 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <core/option.h>
26
27 #include <subdev/i2c.h>
28 #include <subdev/mxm.h>
29 #include <subdev/bios.h>
30 #include <subdev/bios/mxm.h>
31
32 #include "mxms.h"
33
34 static bool
35 mxm_shadow_rom_fetch(struct nouveau_i2c_port *i2c, u8 addr,
36                      u8 offset, u8 size, u8 *data)
37 {
38         struct i2c_msg msgs[] = {
39                 { .addr = addr, .flags = 0, .len = 1, .buf = &offset },
40                 { .addr = addr, .flags = I2C_M_RD, .len = size, .buf = data, },
41         };
42
43         return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
44 }
45
46 static bool
47 mxm_shadow_rom(struct nouveau_mxm *mxm, u8 version)
48 {
49         struct nouveau_bios *bios = nouveau_bios(mxm);
50         struct nouveau_i2c *i2c = nouveau_i2c(mxm);
51         struct nouveau_i2c_port *port = NULL;
52         u8 i2cidx, mxms[6], addr, size;
53
54         i2cidx = mxm_ddc_map(bios, 1 /* LVDS_DDC */) & 0x0f;
55         if (i2cidx < 0x0f)
56                 port = i2c->find(i2c, i2cidx);
57         if (!port)
58                 return false;
59
60         addr = 0x54;
61         if (!mxm_shadow_rom_fetch(port, addr, 0, 6, mxms)) {
62                 addr = 0x56;
63                 if (!mxm_shadow_rom_fetch(port, addr, 0, 6, mxms))
64                         return false;
65         }
66
67         mxm->mxms = mxms;
68         size = mxms_headerlen(mxm) + mxms_structlen(mxm);
69         mxm->mxms = kmalloc(size, GFP_KERNEL);
70
71         if (mxm->mxms &&
72             mxm_shadow_rom_fetch(port, addr, 0, size, mxm->mxms))
73                 return true;
74
75         kfree(mxm->mxms);
76         mxm->mxms = NULL;
77         return false;
78 }
79
80 #if defined(CONFIG_ACPI)
81 static bool
82 mxm_shadow_dsm(struct nouveau_mxm *mxm, u8 version)
83 {
84         struct nouveau_device *device = nv_device(mxm);
85         static char muid[] = {
86                 0x00, 0xA4, 0x04, 0x40, 0x7D, 0x91, 0xF2, 0x4C,
87                 0xB8, 0x9C, 0x79, 0xB6, 0x2F, 0xD5, 0x56, 0x65
88         };
89         u32 mxms_args[] = { 0x00000000 };
90         union acpi_object args[4] = {
91                 /* _DSM MUID */
92                 { .buffer.type = 3,
93                   .buffer.length = sizeof(muid),
94                   .buffer.pointer = muid,
95                 },
96                 /* spec says this can be zero to mean "highest revision", but
97                  * of course there's at least one bios out there which fails
98                  * unless you pass in exactly the version it supports..
99                  */
100                 { .integer.type = ACPI_TYPE_INTEGER,
101                   .integer.value = (version & 0xf0) << 4 | (version & 0x0f),
102                 },
103                 /* MXMS function */
104                 { .integer.type = ACPI_TYPE_INTEGER,
105                   .integer.value = 0x00000010,
106                 },
107                 /* Pointer to MXMS arguments */
108                 { .buffer.type = ACPI_TYPE_BUFFER,
109                   .buffer.length = sizeof(mxms_args),
110                   .buffer.pointer = (char *)mxms_args,
111                 },
112         };
113         struct acpi_object_list list = { ARRAY_SIZE(args), args };
114         struct acpi_buffer retn = { ACPI_ALLOCATE_BUFFER, NULL };
115         union acpi_object *obj;
116         acpi_handle handle;
117         int ret;
118
119         handle = ACPI_HANDLE(&device->pdev->dev);
120         if (!handle)
121                 return false;
122
123         ret = acpi_evaluate_object(handle, "_DSM", &list, &retn);
124         if (ret) {
125                 nv_debug(mxm, "DSM MXMS failed: %d\n", ret);
126                 return false;
127         }
128
129         obj = retn.pointer;
130         if (obj->type == ACPI_TYPE_BUFFER) {
131                 mxm->mxms = kmemdup(obj->buffer.pointer,
132                                          obj->buffer.length, GFP_KERNEL);
133         } else
134         if (obj->type == ACPI_TYPE_INTEGER) {
135                 nv_debug(mxm, "DSM MXMS returned 0x%llx\n", obj->integer.value);
136         }
137
138         kfree(obj);
139         return mxm->mxms != NULL;
140 }
141 #endif
142
143 #if defined(CONFIG_ACPI_WMI) || defined(CONFIG_ACPI_WMI_MODULE)
144
145 #define WMI_WMMX_GUID "F6CB5C3C-9CAE-4EBD-B577-931EA32A2CC0"
146
147 static u8
148 wmi_wmmx_mxmi(struct nouveau_mxm *mxm, u8 version)
149 {
150         u32 mxmi_args[] = { 0x494D584D /* MXMI */, version, 0 };
151         struct acpi_buffer args = { sizeof(mxmi_args), mxmi_args };
152         struct acpi_buffer retn = { ACPI_ALLOCATE_BUFFER, NULL };
153         union acpi_object *obj;
154         acpi_status status;
155
156         status = wmi_evaluate_method(WMI_WMMX_GUID, 0, 0, &args, &retn);
157         if (ACPI_FAILURE(status)) {
158                 nv_debug(mxm, "WMMX MXMI returned %d\n", status);
159                 return 0x00;
160         }
161
162         obj = retn.pointer;
163         if (obj->type == ACPI_TYPE_INTEGER) {
164                 version = obj->integer.value;
165                 nv_debug(mxm, "WMMX MXMI version %d.%d\n",
166                              (version >> 4), version & 0x0f);
167         } else {
168                 version = 0;
169                 nv_debug(mxm, "WMMX MXMI returned non-integer\n");
170         }
171
172         kfree(obj);
173         return version;
174 }
175
176 static bool
177 mxm_shadow_wmi(struct nouveau_mxm *mxm, u8 version)
178 {
179         u32 mxms_args[] = { 0x534D584D /* MXMS */, version, 0 };
180         struct acpi_buffer args = { sizeof(mxms_args), mxms_args };
181         struct acpi_buffer retn = { ACPI_ALLOCATE_BUFFER, NULL };
182         union acpi_object *obj;
183         acpi_status status;
184
185         if (!wmi_has_guid(WMI_WMMX_GUID)) {
186                 nv_debug(mxm, "WMMX GUID not found\n");
187                 return false;
188         }
189
190         mxms_args[1] = wmi_wmmx_mxmi(mxm, 0x00);
191         if (!mxms_args[1])
192                 mxms_args[1] = wmi_wmmx_mxmi(mxm, version);
193         if (!mxms_args[1])
194                 return false;
195
196         status = wmi_evaluate_method(WMI_WMMX_GUID, 0, 0, &args, &retn);
197         if (ACPI_FAILURE(status)) {
198                 nv_debug(mxm, "WMMX MXMS returned %d\n", status);
199                 return false;
200         }
201
202         obj = retn.pointer;
203         if (obj->type == ACPI_TYPE_BUFFER) {
204                 mxm->mxms = kmemdup(obj->buffer.pointer,
205                                          obj->buffer.length, GFP_KERNEL);
206         }
207
208         kfree(obj);
209         return mxm->mxms != NULL;
210 }
211 #endif
212
213 static struct mxm_shadow_h {
214         const char *name;
215         bool (*exec)(struct nouveau_mxm *, u8 version);
216 } _mxm_shadow[] = {
217         { "ROM", mxm_shadow_rom },
218 #if defined(CONFIG_ACPI)
219         { "DSM", mxm_shadow_dsm },
220 #endif
221 #if defined(CONFIG_ACPI_WMI) || defined(CONFIG_ACPI_WMI_MODULE)
222         { "WMI", mxm_shadow_wmi },
223 #endif
224         {}
225 };
226
227 static int
228 mxm_shadow(struct nouveau_mxm *mxm, u8 version)
229 {
230         struct mxm_shadow_h *shadow = _mxm_shadow;
231         do {
232                 nv_debug(mxm, "checking %s\n", shadow->name);
233                 if (shadow->exec(mxm, version)) {
234                         if (mxms_valid(mxm))
235                                 return 0;
236                         kfree(mxm->mxms);
237                         mxm->mxms = NULL;
238                 }
239         } while ((++shadow)->name);
240         return -ENOENT;
241 }
242
243 int
244 nouveau_mxm_create_(struct nouveau_object *parent,
245                     struct nouveau_object *engine,
246                     struct nouveau_oclass *oclass, int length, void **pobject)
247 {
248         struct nouveau_device *device = nv_device(parent);
249         struct nouveau_bios *bios = nouveau_bios(device);
250         struct nouveau_mxm *mxm;
251         u8  ver, len;
252         u16 data;
253         int ret;
254
255         ret = nouveau_subdev_create_(parent, engine, oclass, 0, "MXM", "mxm",
256                                      length, pobject);
257         mxm = *pobject;
258         if (ret)
259                 return ret;
260
261         data = mxm_table(bios, &ver, &len);
262         if (!data || !(ver = nv_ro08(bios, data))) {
263                 nv_debug(mxm, "no VBIOS data, nothing to do\n");
264                 return 0;
265         }
266
267         nv_info(mxm, "BIOS version %d.%d\n", ver >> 4, ver & 0x0f);
268
269         if (mxm_shadow(mxm, ver)) {
270                 nv_info(mxm, "failed to locate valid SIS\n");
271 #if 0
272                 /* we should, perhaps, fall back to some kind of limited
273                  * mode here if the x86 vbios hasn't already done the
274                  * work for us (so we prevent loading with completely
275                  * whacked vbios tables).
276                  */
277                 return -EINVAL;
278 #else
279                 return 0;
280 #endif
281         }
282
283         nv_info(mxm, "MXMS Version %d.%d\n",
284                 mxms_version(mxm) >> 8, mxms_version(mxm) & 0xff);
285         mxms_foreach(mxm, 0, NULL, NULL);
286
287         if (nouveau_boolopt(device->cfgopt, "NvMXMDCB", true))
288                 mxm->action |= MXM_SANITISE_DCB;
289         return 0;
290 }