drm/nouveau/i2c: transition pad/ports away from being based on nvkm_object
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / mxm / base.c
CommitLineData
d38ac521
BS
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 */
be83cd4e 24#include "mxms.h"
d38ac521
BS
25
26#include <core/option.h>
d38ac521
BS
27#include <subdev/bios.h>
28#include <subdev/bios/mxm.h>
be83cd4e 29#include <subdev/i2c.h>
d38ac521
BS
30
31static bool
2aa5eac5 32mxm_shadow_rom_fetch(struct nvkm_i2c_bus *bus, u8 addr,
d38ac521
BS
33 u8 offset, u8 size, u8 *data)
34{
35 struct i2c_msg msgs[] = {
36 { .addr = addr, .flags = 0, .len = 1, .buf = &offset },
37 { .addr = addr, .flags = I2C_M_RD, .len = size, .buf = data, },
38 };
39
2aa5eac5 40 return i2c_transfer(&bus->i2c, msgs, 2) == 2;
d38ac521
BS
41}
42
43static bool
be83cd4e 44mxm_shadow_rom(struct nvkm_mxm *mxm, u8 version)
d38ac521 45{
be83cd4e
BS
46 struct nvkm_bios *bios = nvkm_bios(mxm);
47 struct nvkm_i2c *i2c = nvkm_i2c(mxm);
2aa5eac5 48 struct nvkm_i2c_bus *bus = NULL;
d38ac521
BS
49 u8 i2cidx, mxms[6], addr, size;
50
51 i2cidx = mxm_ddc_map(bios, 1 /* LVDS_DDC */) & 0x0f;
52 if (i2cidx < 0x0f)
2aa5eac5
BS
53 bus = nvkm_i2c_bus_find(i2c, i2cidx);
54 if (!bus)
d38ac521
BS
55 return false;
56
57 addr = 0x54;
2aa5eac5 58 if (!mxm_shadow_rom_fetch(bus, addr, 0, 6, mxms)) {
d38ac521 59 addr = 0x56;
2aa5eac5 60 if (!mxm_shadow_rom_fetch(bus, addr, 0, 6, mxms))
d38ac521
BS
61 return false;
62 }
63
64 mxm->mxms = mxms;
65 size = mxms_headerlen(mxm) + mxms_structlen(mxm);
66 mxm->mxms = kmalloc(size, GFP_KERNEL);
67
68 if (mxm->mxms &&
2aa5eac5 69 mxm_shadow_rom_fetch(bus, addr, 0, size, mxm->mxms))
d38ac521
BS
70 return true;
71
72 kfree(mxm->mxms);
73 mxm->mxms = NULL;
74 return false;
75}
76
77#if defined(CONFIG_ACPI)
78static bool
be83cd4e 79mxm_shadow_dsm(struct nvkm_mxm *mxm, u8 version)
d38ac521 80{
27cc60a1
BS
81 struct nvkm_subdev *subdev = &mxm->subdev;
82 struct nvkm_device *device = subdev->device;
d38ac521
BS
83 static char muid[] = {
84 0x00, 0xA4, 0x04, 0x40, 0x7D, 0x91, 0xF2, 0x4C,
85 0xB8, 0x9C, 0x79, 0xB6, 0x2F, 0xD5, 0x56, 0x65
86 };
87 u32 mxms_args[] = { 0x00000000 };
b072e53b
JL
88 union acpi_object argv4 = {
89 .buffer.type = ACPI_TYPE_BUFFER,
90 .buffer.length = sizeof(mxms_args),
91 .buffer.pointer = (char *)mxms_args,
d38ac521 92 };
d38ac521
BS
93 union acpi_object *obj;
94 acpi_handle handle;
b072e53b 95 int rev;
d38ac521 96
420b9469 97 handle = ACPI_HANDLE(nv_device_base(device));
d38ac521
BS
98 if (!handle)
99 return false;
100
b072e53b
JL
101 /*
102 * spec says this can be zero to mean "highest revision", but
103 * of course there's at least one bios out there which fails
104 * unless you pass in exactly the version it supports..
105 */
106 rev = (version & 0xf0) << 4 | (version & 0x0f);
107 obj = acpi_evaluate_dsm(handle, muid, rev, 0x00000010, &argv4);
108 if (!obj) {
27cc60a1 109 nvkm_debug(subdev, "DSM MXMS failed\n");
d38ac521
BS
110 return false;
111 }
112
d38ac521
BS
113 if (obj->type == ACPI_TYPE_BUFFER) {
114 mxm->mxms = kmemdup(obj->buffer.pointer,
115 obj->buffer.length, GFP_KERNEL);
b072e53b 116 } else if (obj->type == ACPI_TYPE_INTEGER) {
27cc60a1
BS
117 nvkm_debug(subdev, "DSM MXMS returned 0x%llx\n",
118 obj->integer.value);
d38ac521
BS
119 }
120
b072e53b 121 ACPI_FREE(obj);
d38ac521
BS
122 return mxm->mxms != NULL;
123}
124#endif
125
126#if defined(CONFIG_ACPI_WMI) || defined(CONFIG_ACPI_WMI_MODULE)
127
128#define WMI_WMMX_GUID "F6CB5C3C-9CAE-4EBD-B577-931EA32A2CC0"
129
130static u8
be83cd4e 131wmi_wmmx_mxmi(struct nvkm_mxm *mxm, u8 version)
d38ac521 132{
27cc60a1 133 struct nvkm_subdev *subdev = &mxm->subdev;
d38ac521
BS
134 u32 mxmi_args[] = { 0x494D584D /* MXMI */, version, 0 };
135 struct acpi_buffer args = { sizeof(mxmi_args), mxmi_args };
136 struct acpi_buffer retn = { ACPI_ALLOCATE_BUFFER, NULL };
137 union acpi_object *obj;
138 acpi_status status;
139
140 status = wmi_evaluate_method(WMI_WMMX_GUID, 0, 0, &args, &retn);
141 if (ACPI_FAILURE(status)) {
27cc60a1 142 nvkm_debug(subdev, "WMMX MXMI returned %d\n", status);
d38ac521
BS
143 return 0x00;
144 }
145
146 obj = retn.pointer;
147 if (obj->type == ACPI_TYPE_INTEGER) {
148 version = obj->integer.value;
27cc60a1
BS
149 nvkm_debug(subdev, "WMMX MXMI version %d.%d\n",
150 (version >> 4), version & 0x0f);
d38ac521
BS
151 } else {
152 version = 0;
27cc60a1 153 nvkm_debug(subdev, "WMMX MXMI returned non-integer\n");
d38ac521
BS
154 }
155
156 kfree(obj);
157 return version;
158}
159
160static bool
be83cd4e 161mxm_shadow_wmi(struct nvkm_mxm *mxm, u8 version)
d38ac521 162{
27cc60a1 163 struct nvkm_subdev *subdev = &mxm->subdev;
d38ac521
BS
164 u32 mxms_args[] = { 0x534D584D /* MXMS */, version, 0 };
165 struct acpi_buffer args = { sizeof(mxms_args), mxms_args };
166 struct acpi_buffer retn = { ACPI_ALLOCATE_BUFFER, NULL };
167 union acpi_object *obj;
168 acpi_status status;
169
170 if (!wmi_has_guid(WMI_WMMX_GUID)) {
27cc60a1 171 nvkm_debug(subdev, "WMMX GUID not found\n");
d38ac521
BS
172 return false;
173 }
174
175 mxms_args[1] = wmi_wmmx_mxmi(mxm, 0x00);
176 if (!mxms_args[1])
177 mxms_args[1] = wmi_wmmx_mxmi(mxm, version);
178 if (!mxms_args[1])
179 return false;
180
181 status = wmi_evaluate_method(WMI_WMMX_GUID, 0, 0, &args, &retn);
182 if (ACPI_FAILURE(status)) {
27cc60a1 183 nvkm_debug(subdev, "WMMX MXMS returned %d\n", status);
d38ac521
BS
184 return false;
185 }
186
187 obj = retn.pointer;
188 if (obj->type == ACPI_TYPE_BUFFER) {
189 mxm->mxms = kmemdup(obj->buffer.pointer,
be83cd4e 190 obj->buffer.length, GFP_KERNEL);
d38ac521
BS
191 }
192
193 kfree(obj);
194 return mxm->mxms != NULL;
195}
196#endif
197
5b8a43ae 198static struct mxm_shadow_h {
d38ac521 199 const char *name;
be83cd4e 200 bool (*exec)(struct nvkm_mxm *, u8 version);
d38ac521
BS
201} _mxm_shadow[] = {
202 { "ROM", mxm_shadow_rom },
203#if defined(CONFIG_ACPI)
204 { "DSM", mxm_shadow_dsm },
205#endif
206#if defined(CONFIG_ACPI_WMI) || defined(CONFIG_ACPI_WMI_MODULE)
207 { "WMI", mxm_shadow_wmi },
208#endif
209 {}
210};
211
212static int
be83cd4e 213mxm_shadow(struct nvkm_mxm *mxm, u8 version)
d38ac521
BS
214{
215 struct mxm_shadow_h *shadow = _mxm_shadow;
216 do {
27cc60a1 217 nvkm_debug(&mxm->subdev, "checking %s\n", shadow->name);
d38ac521
BS
218 if (shadow->exec(mxm, version)) {
219 if (mxms_valid(mxm))
220 return 0;
221 kfree(mxm->mxms);
222 mxm->mxms = NULL;
223 }
224 } while ((++shadow)->name);
225 return -ENOENT;
226}
227
228int
be83cd4e
BS
229nvkm_mxm_create_(struct nvkm_object *parent, struct nvkm_object *engine,
230 struct nvkm_oclass *oclass, int length, void **pobject)
d38ac521 231{
be83cd4e
BS
232 struct nvkm_device *device = nv_device(parent);
233 struct nvkm_bios *bios = nvkm_bios(device);
234 struct nvkm_mxm *mxm;
d38ac521
BS
235 u8 ver, len;
236 u16 data;
237 int ret;
238
be83cd4e
BS
239 ret = nvkm_subdev_create_(parent, engine, oclass, 0, "MXM", "mxm",
240 length, pobject);
d38ac521
BS
241 mxm = *pobject;
242 if (ret)
243 return ret;
244
245 data = mxm_table(bios, &ver, &len);
7f5f518f 246 if (!data || !(ver = nvbios_rd08(bios, data))) {
27cc60a1 247 nvkm_debug(&mxm->subdev, "no VBIOS data, nothing to do\n");
d38ac521
BS
248 return 0;
249 }
250
27cc60a1 251 nvkm_info(&mxm->subdev, "BIOS version %d.%d\n", ver >> 4, ver & 0x0f);
d38ac521
BS
252
253 if (mxm_shadow(mxm, ver)) {
27cc60a1 254 nvkm_warn(&mxm->subdev, "failed to locate valid SIS\n");
d38ac521
BS
255#if 0
256 /* we should, perhaps, fall back to some kind of limited
257 * mode here if the x86 vbios hasn't already done the
258 * work for us (so we prevent loading with completely
259 * whacked vbios tables).
260 */
261 return -EINVAL;
262#else
263 return 0;
264#endif
265 }
266
27cc60a1
BS
267 nvkm_debug(&mxm->subdev, "MXMS Version %d.%d\n",
268 mxms_version(mxm) >> 8, mxms_version(mxm) & 0xff);
d38ac521
BS
269 mxms_foreach(mxm, 0, NULL, NULL);
270
be83cd4e 271 if (nvkm_boolopt(device->cfgopt, "NvMXMDCB", true))
d38ac521
BS
272 mxm->action |= MXM_SANITISE_DCB;
273 return 0;
274}
This page took 0.263982 seconds and 5 git commands to generate.