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