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