1/*
2 * Copyright 2012 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 */
23#include "priv.h"
24
25#include <core/device.h>
26
27#if defined(CONFIG_ACPI) && defined(CONFIG_X86)
28int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len);
29bool nouveau_acpi_rom_supported(struct pci_dev *pdev);
30#else
31static inline bool
32nouveau_acpi_rom_supported(struct pci_dev *pdev)
33{
34	return false;
35}
36
37static inline int
38nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
39{
40	return -EINVAL;
41}
42#endif
43
44/* This version of the shadow function disobeys the ACPI spec and tries
45 * to fetch in units of more than 4KiB at a time.  This is a LOT faster
46 * on some systems, such as Lenovo W530.
47 */
48static u32
49acpi_read_fast(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
50{
51	u32 limit = (offset + length + 0xfff) & ~0xfff;
52	u32 start = offset & ~0x00000fff;
53	u32 fetch = limit - start;
54
55	if (nvbios_extend(bios, limit) >= 0) {
56		int ret = nouveau_acpi_get_bios_chunk(bios->data, start, fetch);
57		if (ret == fetch)
58			return fetch;
59	}
60
61	return 0;
62}
63
64/* Other systems, such as the one in fdo#55948, will report a success
65 * but only return 4KiB of data.  The common bios fetching logic will
66 * detect an invalid image, and fall back to this version of the read
67 * function.
68 */
69static u32
70acpi_read_slow(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
71{
72	u32 limit = (offset + length + 0xfff) & ~0xfff;
73	u32 start = offset & ~0xfff;
74	u32 fetch = 0;
75
76	if (nvbios_extend(bios, limit) >= 0) {
77		while (start + fetch < limit) {
78			int ret = nouveau_acpi_get_bios_chunk(bios->data,
79							      start + fetch,
80							      0x1000);
81			if (ret != 0x1000)
82				break;
83			fetch += 0x1000;
84		}
85	}
86
87	return fetch;
88}
89
90static void *
91acpi_init(struct nvkm_bios *bios, const char *name)
92{
93	if (!nouveau_acpi_rom_supported(nv_device(bios)->pdev))
94		return ERR_PTR(-ENODEV);
95	return NULL;
96}
97
98const struct nvbios_source
99nvbios_acpi_fast = {
100	.name = "ACPI",
101	.init = acpi_init,
102	.read = acpi_read_fast,
103	.rw = false,
104};
105
106const struct nvbios_source
107nvbios_acpi_slow = {
108	.name = "ACPI",
109	.init = acpi_init,
110	.read = acpi_read_slow,
111	.rw = false,
112};
113