1/*
2 * Copyright 2014 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 <bskeggs@redhat.com>
23 */
24#include <subdev/bios.h>
25#include <subdev/bios/bit.h>
26#include <subdev/bios/image.h>
27#include <subdev/bios/pmu.h>
28
29static u32
30weirdo_pointer(struct nvkm_bios *bios, u32 data)
31{
32	struct nvbios_image image;
33	int idx = 0;
34	if (nvbios_image(bios, idx++, &image)) {
35		data -= image.size;
36		while (nvbios_image(bios, idx++, &image)) {
37			if (image.type == 0xe0)
38				return image.base + data;
39		}
40	}
41	return 0;
42}
43
44u32
45nvbios_pmuTe(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
46{
47	struct bit_entry bit_p;
48	u32 data = 0;
49
50	if (!bit_entry(bios, 'p', &bit_p)) {
51		if (bit_p.version == 2 && bit_p.length >= 4)
52			data = nv_ro32(bios, bit_p.offset + 0x00);
53		if ((data = weirdo_pointer(bios, data))) {
54			*ver = nv_ro08(bios, data + 0x00); /* maybe? */
55			*hdr = nv_ro08(bios, data + 0x01);
56			*len = nv_ro08(bios, data + 0x02);
57			*cnt = nv_ro08(bios, data + 0x03);
58		}
59	}
60
61	return data;
62}
63
64u32
65nvbios_pmuTp(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
66	     struct nvbios_pmuT *info)
67{
68	u32 data = nvbios_pmuTe(bios, ver, hdr, cnt, len);
69	memset(info, 0x00, sizeof(*info));
70	switch (!!data * *ver) {
71	default:
72		break;
73	}
74	return data;
75}
76
77u32
78nvbios_pmuEe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
79{
80	u8  cnt, len;
81	u32 data = nvbios_pmuTe(bios, ver, hdr, &cnt, &len);
82	if (data && idx < cnt) {
83		data = data + *hdr + (idx * len);
84		*hdr = len;
85		return data;
86	}
87	return 0;
88}
89
90u32
91nvbios_pmuEp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
92	     struct nvbios_pmuE *info)
93{
94	u32 data = nvbios_pmuEe(bios, idx, ver, hdr);
95	memset(info, 0x00, sizeof(*info));
96	switch (!!data * *ver) {
97	default:
98		info->type = nv_ro08(bios, data + 0x00);
99		info->data = nv_ro32(bios, data + 0x02);
100		break;
101	}
102	return data;
103}
104
105bool
106nvbios_pmuRm(struct nvkm_bios *bios, u8 type, struct nvbios_pmuR *info)
107{
108	struct nvbios_pmuE pmuE;
109	u8  ver, hdr, idx = 0;
110	u32 data;
111	memset(info, 0x00, sizeof(*info));
112	while ((data = nvbios_pmuEp(bios, idx++, &ver, &hdr, &pmuE))) {
113		if ( pmuE.type == type &&
114		    (data = weirdo_pointer(bios, pmuE.data))) {
115			info->init_addr_pmu = nv_ro32(bios, data + 0x08);
116			info->args_addr_pmu = nv_ro32(bios, data + 0x0c);
117			info->boot_addr     = data + 0x30;
118			info->boot_addr_pmu = nv_ro32(bios, data + 0x10) +
119					      nv_ro32(bios, data + 0x18);
120			info->boot_size     = nv_ro32(bios, data + 0x1c) -
121					      nv_ro32(bios, data + 0x18);
122			info->code_addr     = info->boot_addr + info->boot_size;
123			info->code_addr_pmu = info->boot_addr_pmu +
124					      info->boot_size;
125			info->code_size     = nv_ro32(bios, data + 0x20);
126			info->data_addr     = data + 0x30 +
127					      nv_ro32(bios, data + 0x24);
128			info->data_addr_pmu = nv_ro32(bios, data + 0x28);
129			info->data_size     = nv_ro32(bios, data + 0x2c);
130			return true;
131		}
132	}
133	return false;
134}
135