xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_vpstate.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_bios_vpstate.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2016 Karol Herbst
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: Karol Herbst
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_vpstate.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $");
28 
29 #include <subdev/bios.h>
30 #include <subdev/bios/bit.h>
31 #include <subdev/bios/vpstate.h>
32 
33 static u32
nvbios_vpstate_offset(struct nvkm_bios * b)34 nvbios_vpstate_offset(struct nvkm_bios *b)
35 {
36 	struct bit_entry bit_P;
37 
38 	if (!bit_entry(b, 'P', &bit_P)) {
39 		if (bit_P.version == 2 && bit_P.length >= 0x3c)
40 			return nvbios_rd32(b, bit_P.offset + 0x38);
41 	}
42 
43 	return 0x0000;
44 }
45 
46 int
nvbios_vpstate_parse(struct nvkm_bios * b,struct nvbios_vpstate_header * h)47 nvbios_vpstate_parse(struct nvkm_bios *b, struct nvbios_vpstate_header *h)
48 {
49 	if (!h)
50 		return -EINVAL;
51 
52 	h->offset = nvbios_vpstate_offset(b);
53 	if (!h->offset)
54 		return -ENODEV;
55 
56 	h->version = nvbios_rd08(b, h->offset);
57 	switch (h->version) {
58 	case 0x10:
59 		h->hlen     = nvbios_rd08(b, h->offset + 0x1);
60 		h->elen     = nvbios_rd08(b, h->offset + 0x2);
61 		h->slen     = nvbios_rd08(b, h->offset + 0x3);
62 		h->scount   = nvbios_rd08(b, h->offset + 0x4);
63 		h->ecount   = nvbios_rd08(b, h->offset + 0x5);
64 
65 		h->base_id  = nvbios_rd08(b, h->offset + 0x0f);
66 		if (h->hlen > 0x10)
67 			h->boost_id = nvbios_rd08(b, h->offset + 0x10);
68 		else
69 			h->boost_id = 0xff;
70 		if (h->hlen > 0x11)
71 			h->tdp_id = nvbios_rd08(b, h->offset + 0x11);
72 		else
73 			h->tdp_id = 0xff;
74 		return 0;
75 	default:
76 		return -EINVAL;
77 	}
78 }
79 
80 int
nvbios_vpstate_entry(struct nvkm_bios * b,struct nvbios_vpstate_header * h,u8 idx,struct nvbios_vpstate_entry * e)81 nvbios_vpstate_entry(struct nvkm_bios *b, struct nvbios_vpstate_header *h,
82 		     u8 idx, struct nvbios_vpstate_entry *e)
83 {
84 	u32 offset;
85 
86 	if (!e || !h || idx > h->ecount)
87 		return -EINVAL;
88 
89 	offset = h->offset + h->hlen + idx * (h->elen + (h->slen * h->scount));
90 	e->pstate    = nvbios_rd08(b, offset);
91 	e->clock_mhz = nvbios_rd16(b, offset + 0x5);
92 	return 0;
93 }
94