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