1 /*	$NetBSD: nouveau_subdev_bios_boost.c,v 1.1.1.1 2014/08/06 12:36:28 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2013 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
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bios_boost.c,v 1.1.1.1 2014/08/06 12:36:28 riastradh Exp $");
29 
30 #include <subdev/bios.h>
31 #include <subdev/bios/bit.h>
32 #include <subdev/bios/boost.h>
33 
34 u16
nvbios_boostTe(struct nouveau_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,u8 * snr,u8 * ssz)35 nvbios_boostTe(struct nouveau_bios *bios,
36 	       u8 *ver, u8 *hdr, u8 *cnt, u8 *len, u8 *snr, u8 *ssz)
37 {
38 	struct bit_entry bit_P;
39 	u16 boost = 0x0000;
40 
41 	if (!bit_entry(bios, 'P', &bit_P)) {
42 		if (bit_P.version == 2)
43 			boost = nv_ro16(bios, bit_P.offset + 0x30);
44 
45 		if (boost) {
46 			*ver = nv_ro08(bios, boost + 0);
47 			switch (*ver) {
48 			case 0x11:
49 				*hdr = nv_ro08(bios, boost + 1);
50 				*cnt = nv_ro08(bios, boost + 5);
51 				*len = nv_ro08(bios, boost + 2);
52 				*snr = nv_ro08(bios, boost + 4);
53 				*ssz = nv_ro08(bios, boost + 3);
54 				return boost;
55 			default:
56 				break;
57 			}
58 		}
59 	}
60 
61 	return 0x0000;
62 }
63 
64 u16
nvbios_boostEe(struct nouveau_bios * bios,int idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)65 nvbios_boostEe(struct nouveau_bios *bios, int idx,
66 	       u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
67 {
68 	u8  snr, ssz;
69 	u16 data = nvbios_boostTe(bios, ver, hdr, cnt, len, &snr, &ssz);
70 	if (data && idx < *cnt) {
71 		data = data + *hdr + (idx * (*len + (snr * ssz)));
72 		*hdr = *len;
73 		*cnt = snr;
74 		*len = ssz;
75 		return data;
76 	}
77 	return 0x0000;
78 }
79 
80 u16
nvbios_boostEp(struct nouveau_bios * bios,int idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_boostE * info)81 nvbios_boostEp(struct nouveau_bios *bios, int idx,
82 	       u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_boostE *info)
83 {
84 	u16 data = nvbios_boostEe(bios, idx, ver, hdr, cnt, len);
85 	memset(info, 0x00, sizeof(*info));
86 	if (data) {
87 		info->pstate = (nv_ro16(bios, data + 0x00) & 0x01e0) >> 5;
88 		info->min    =  nv_ro16(bios, data + 0x02) * 1000;
89 		info->max    =  nv_ro16(bios, data + 0x04) * 1000;
90 	}
91 	return data;
92 }
93 
94 u16
nvbios_boostEm(struct nouveau_bios * bios,u8 pstate,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_boostE * info)95 nvbios_boostEm(struct nouveau_bios *bios, u8 pstate,
96 	       u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_boostE *info)
97 {
98 	u32 data, idx = 0;
99 	while ((data = nvbios_boostEp(bios, idx++, ver, hdr, cnt, len, info))) {
100 		if (info->pstate == pstate)
101 			break;
102 	}
103 	return data;
104 }
105 
106 u16
nvbios_boostSe(struct nouveau_bios * bios,int idx,u16 data,u8 * ver,u8 * hdr,u8 cnt,u8 len)107 nvbios_boostSe(struct nouveau_bios *bios, int idx,
108 	       u16 data, u8 *ver, u8 *hdr, u8 cnt, u8 len)
109 {
110 	if (data && idx < cnt) {
111 		data = data + *hdr + (idx * len);
112 		*hdr = len;
113 		return data;
114 	}
115 	return 0x0000;
116 }
117 
118 u16
nvbios_boostSp(struct nouveau_bios * bios,int idx,u16 data,u8 * ver,u8 * hdr,u8 cnt,u8 len,struct nvbios_boostS * info)119 nvbios_boostSp(struct nouveau_bios *bios, int idx,
120 	       u16 data, u8 *ver, u8 *hdr, u8 cnt, u8 len,
121 	       struct nvbios_boostS *info)
122 {
123 	data = nvbios_boostSe(bios, idx, data, ver, hdr, cnt, len);
124 	memset(info, 0x00, sizeof(*info));
125 	if (data) {
126 		info->domain  = nv_ro08(bios, data + 0x00);
127 		info->percent = nv_ro08(bios, data + 0x01);
128 		info->min     = nv_ro16(bios, data + 0x02) * 1000;
129 		info->max     = nv_ro16(bios, data + 0x04) * 1000;
130 	}
131 	return data;
132 }
133