1/*	$NetBSD: nouveau_nvkm_subdev_bios_pmu.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $	*/
2
3/*
4 * Copyright 2014 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_pmu.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
28
29#include <subdev/bios.h>
30#include <subdev/bios/bit.h>
31#include <subdev/bios/image.h>
32#include <subdev/bios/pmu.h>
33
34u32
35nvbios_pmuTe(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
36{
37	struct bit_entry bit_p;
38	u32 data = 0;
39
40	if (!bit_entry(bios, 'p', &bit_p)) {
41		if (bit_p.version == 2 && bit_p.length >= 4)
42			data = nvbios_rd32(bios, bit_p.offset + 0x00);
43		if (data) {
44			*ver = nvbios_rd08(bios, data + 0x00); /* maybe? */
45			*hdr = nvbios_rd08(bios, data + 0x01);
46			*len = nvbios_rd08(bios, data + 0x02);
47			*cnt = nvbios_rd08(bios, data + 0x03);
48		}
49	}
50
51	return data;
52}
53
54u32
55nvbios_pmuEe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
56{
57	u8  cnt, len;
58	u32 data = nvbios_pmuTe(bios, ver, hdr, &cnt, &len);
59	if (data && idx < cnt) {
60		data = data + *hdr + (idx * len);
61		*hdr = len;
62		return data;
63	}
64	return 0;
65}
66
67u32
68nvbios_pmuEp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
69	     struct nvbios_pmuE *info)
70{
71	u32 data = nvbios_pmuEe(bios, idx, ver, hdr);
72	memset(info, 0x00, sizeof(*info));
73	switch (!!data * *ver) {
74	default:
75		info->type = nvbios_rd08(bios, data + 0x00);
76		info->data = nvbios_rd32(bios, data + 0x02);
77		break;
78	}
79	return data;
80}
81
82bool
83nvbios_pmuRm(struct nvkm_bios *bios, u8 type, struct nvbios_pmuR *info)
84{
85	struct nvbios_pmuE pmuE;
86	u8  ver, hdr, idx = 0;
87	u32 data;
88	memset(info, 0x00, sizeof(*info));
89	while ((data = nvbios_pmuEp(bios, idx++, &ver, &hdr, &pmuE))) {
90		if (pmuE.type == type && (data = pmuE.data)) {
91			info->init_addr_pmu = nvbios_rd32(bios, data + 0x08);
92			info->args_addr_pmu = nvbios_rd32(bios, data + 0x0c);
93			info->boot_addr     = data + 0x30;
94			info->boot_addr_pmu = nvbios_rd32(bios, data + 0x10) +
95					      nvbios_rd32(bios, data + 0x18);
96			info->boot_size     = nvbios_rd32(bios, data + 0x1c) -
97					      nvbios_rd32(bios, data + 0x18);
98			info->code_addr     = info->boot_addr + info->boot_size;
99			info->code_addr_pmu = info->boot_addr_pmu +
100					      info->boot_size;
101			info->code_size     = nvbios_rd32(bios, data + 0x20);
102			info->data_addr     = data + 0x30 +
103					      nvbios_rd32(bios, data + 0x24);
104			info->data_addr_pmu = nvbios_rd32(bios, data + 0x28);
105			info->data_size     = nvbios_rd32(bios, data + 0x2c);
106			return true;
107		}
108	}
109	return false;
110}
111