Skip to contentSkip to conversation

Expose power budget cap via hwmon

PATCH Superseded Karol Herbst 2 participants 9 messages Nov 30, 2016 5 messages not in the archive

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Expose power budget cap via hwmon

There is an optinal header field in the power budget table we can use to read out the power cap of the GPU.

We should have this in the kernel before actually using it to catch errors and see how reliable this is, but as it seems it works on all GPUs as expected on Kepler und Maxwells with the power cap field set in the vbios.

This series keeps things really simple for now until we figure out more about the power budget table.

v3: big rework of the actual vbios parsing to prevent memory leakage and
silly memory handling

Karol Herbst (3):
nvbios/power_budget: Add basic power budget parsing subdev/iccsense: Parse max and crit power level
hwmon: expose power_max and power_crit

 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/include/nvkm/subdev/iccsense.h         |   3 +
 drm/nouveau/nouveau_hwmon.c                        |  44 ++++++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 drm/nouveau/nvkm/subdev/iccsense/base.c            |  20 +++-
 6 files changed, 216 insertions(+), 2 deletions(-)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
Signature · 6 lines
2.11.0.rc2

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#2 lore raw

PATCH v3 1/3 nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+	u32 min_w;
+	u32 avg_w;
+	u32 max_w;
+};
+
+struct nvbios_power_budget {
+	u32 offset;
+	u8  hlen;
+	u8  elen;
+	u8  ecount;
+	u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +125 -0

125 lines not on this page · raw message

Signature · 6 lines
2.11.0.rc2

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#12 lore raw

PATCH v3 2/3 subdev/iccsense: Parse max and crit power level

In reply to a message not in the archive
---
 drm/nouveau/include/nvkm/subdev/iccsense.h |  3 +++
 drm/nouveau/nvkm/subdev/iccsense/base.c    | 20 ++++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
drm/nouveau/include/nvkm/subdev/iccsense.h +3 -0
index 3c2ddd97..b7a9b041 100644
@@ -8,6 +8,9 @@ struct nvkm_iccsense {
 	bool data_valid;
 	struct list_head sensors;
 	struct list_head rails;
+
+	u32 power_w_max;
+	u32 power_w_crit;
 };
 
 int gf100_iccsense_new(struct nvkm_device *, int index, struct nvkm_iccsense **);
drm/nouveau/nvkm/subdev/iccsense/base.c +18 -2
index f0af2a38..fecfa6af 100644
@@ -26,6 +26,7 @@
 #include <subdev/bios.h>
 #include <subdev/bios/extdev.h>
 #include <subdev/bios/iccsense.h>
+#include <subdev/bios/power_budget.h>
 #include <subdev/i2c.h>
 
 static bool
@@ -216,10 +217,25 @@ nvkm_iccsense_oneinit(struct nvkm_subdev *subdev)
 {
 	struct nvkm_iccsense *iccsense = nvkm_iccsense(subdev);
 	struct nvkm_bios *bios = subdev->device->bios;
+	struct nvbios_power_budget budget;
 	struct nvbios_iccsense stbl;
-	int i;
+	int i, ret;
 
-	if (!bios || nvbios_iccsense_parse(bios, &stbl) || !stbl.nr_entry)
+	if (!bios)
+		return 0;
+
+	ret = nvbios_power_budget_header(bios, &budget);
+	if (!ret && budget.cap_entry != 0xff) {
+		struct nvbios_power_budget_entry entry;
+		ret = nvbios_power_budget_entry(bios, &budget,
+		                                budget.cap_entry, &entry);
+		if (!ret) {
+			iccsense->power_w_max  = entry.avg_w;
+			iccsense->power_w_crit = entry.max_w;
+		}
+	}
+
+	if (nvbios_iccsense_parse(bios, &stbl) || !stbl.nr_entry)
 		return 0;
 
 	iccsense->data_valid = true;
Signature · 6 lines
2.11.0.rc2

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#13 lore raw

PATCH v3 3/3 hwmon: expose power_max and power_crit

In reply to a message not in the archive
---
 drm/nouveau/nouveau_hwmon.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drm/nouveau/nouveau_hwmon.c +44 -0
index 71f764bf..3d4672ae 100644
@@ -596,6 +596,32 @@ nouveau_hwmon_get_power1_input(struct device *d, struct device_attribute *a,
 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO,
 			  nouveau_hwmon_get_power1_input, NULL, 0);
 
+static ssize_t
+nouveau_hwmon_get_power1_max(struct device *d, struct device_attribute *a,
+			     char *buf)
+{
+	struct drm_device *dev = dev_get_drvdata(d);
+	struct nouveau_drm *drm = nouveau_drm(dev);
+	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
+	return sprintf(buf, "%i\n", iccsense->power_w_max);
+}
+
+static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO,
+			  nouveau_hwmon_get_power1_max, NULL, 0);
+
+static ssize_t
+nouveau_hwmon_get_power1_crit(struct device *d, struct device_attribute *a,
+			      char *buf)
+{
+	struct drm_device *dev = dev_get_drvdata(d);
+	struct nouveau_drm *drm = nouveau_drm(dev);
+	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
+	return sprintf(buf, "%i\n", iccsense->power_w_crit);
+}
+
+static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO,
+			  nouveau_hwmon_get_power1_crit, NULL, 0);
+
 static struct attribute *hwmon_default_attributes[] = {
 	&sensor_dev_attr_name.dev_attr.attr,
 	&sensor_dev_attr_update_rate.dev_attr.attr,
@@ -639,6 +665,12 @@ static struct attribute *hwmon_power_attributes[] = {
 	NULL
 };
 
+static struct attribute *hwmon_power_caps_attributes[] = {
+	&sensor_dev_attr_power1_max.dev_attr.attr,
+	&sensor_dev_attr_power1_crit.dev_attr.attr,
+	NULL
+};
+
 static const struct attribute_group hwmon_default_attrgroup = {
 	.attrs = hwmon_default_attributes,
 };
@@ -657,6 +689,9 @@ static const struct attribute_group hwmon_in0_attrgroup = {
 static const struct attribute_group hwmon_power_attrgroup = {
 	.attrs = hwmon_power_attributes,
 };
+static const struct attribute_group hwmon_power_caps_attrgroup = {
+	.attrs = hwmon_power_caps_attributes,
+};
 #endif
 
 int
@@ -728,8 +763,16 @@ nouveau_hwmon_init(struct drm_device *dev)
 	if (iccsense && iccsense->data_valid && !list_empty(&iccsense->rails)) {
 		ret = sysfs_create_group(&hwmon_dev->kobj,
 					 &hwmon_power_attrgroup);
+
 		if (ret)
 			goto error;
+
+		if (iccsense->power_w_max && iccsense->power_w_crit) {
+			ret = sysfs_create_group(&hwmon_dev->kobj,
+						 &hwmon_power_caps_attrgroup);
+			if (ret)
+				goto error;
+		}
 	}
 
 	hwmon->hwmon = hwmon_dev;
@@ -759,6 +802,7 @@ nouveau_hwmon_fini(struct drm_device *dev)
 		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
 		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_in0_attrgroup);
 		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_attrgroup);
+		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_caps_attrgroup);
 
 		hwmon_device_unregister(hwmon->hwmon);
 	}
Signature · 6 lines
2.11.0.rc2

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#4 lore raw

Re: [PATCH v3 1/3] nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive
Quoted 168 lines · Karol Herbst

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+	u32 min_w;
+	u32 avg_w;
+	u32 max_w;
+};
+
+struct nvbios_power_budget {
+	u32 offset;
+	u8  hlen;
+	u8  elen;
+	u8  ecount;
+	u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +104 -0
new file mode 100644
index 00000000..04c69095
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst
+ */
+#include <subdev/bios.h>
+#include <subdev/bios/bit.h>
+#include <subdev/bios/power_budget.h>
+
+static u32
+nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
+			  u8 *len)
+{
+	struct bit_entry bit_P;
+	u32 power_budget;
+
+	if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
+	    bit_P.length < 0x2c)
+		return 0;
+
+	power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
+	if (!power_budget)
+		return 0;
+
+	*ver = nvbios_rd08(bios, power_budget);
+	switch (*ver) {
+	case 0x10:
+	case 0x20:
+	case 0x30:
+		*hdr = nvbios_rd08(bios, power_budget + 0x1);
+		*len = nvbios_rd08(bios, power_budget + 0x2);
+		*cnt = nvbios_rd08(bios, power_budget + 0x3);
+		return power_budget;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+int
+nvbios_power_budget_header(struct nvkm_bios *bios,
+                           struct nvbios_power_budget *budget)
+{
+	struct nvkm_subdev *subdev = &bios->subdev;
+	u8 ver, hdr, cnt, len, cap_entry;
+	u32 header;
+
+	if (!bios || !budget)
+		return -EINVAL;
+
+	header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
+	if (!header || !cnt)
+		return -ENODEV;
+
+	budget->offset = header;
+	budget->hlen = hdr;
+	budget->elen = len;
+	budget->ecount = cnt;
+
+	switch (ver) {
+	case 0x20:
+		cap_entry = nvbios_rd08(bios, header + 0x9);
+		break;
+	case 0x30:
+		cap_entry = nvbios_rd08(bios, header + 0xa);
+		break;
+	default:
+		cap_entry = 0xff;
+	}
+
+	if (cap_entry >= cnt && cap_entry != 0xff) {
+		nvkm_warn(subdev,
+		          "invalid cap_entry in power budget table found\n");
+		budget->cap_entry = 0xff;
+		return -EINVAL;
+	}
+
+	budget->cap_entry = cap_entry;
+	return 0;
+}
+
+int
+nvbios_power_budget_entry(struct nvkm_bios *bios,
+                          struct nvbios_power_budget *budget,
+                          u8 idx, struct nvbios_power_budget_entry *entry)

drm/nouveau/nvkm/subdev/bios/power_budget.c line 114

Quoted 10 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +10 -0
+{
+	u32 entry_offset;
+
+	if (!bios || !budget || !budget->offset || idx >= budget->ecount
+		|| !entry)
+		return -EINVAL;
+
+	entry_offset = budget->offset + budget->hlen + idx * budget->elen;
+
+	if (budget->elen >= 0xe) {

I question this check. Generally, when the layout of existing fields changes, there's a version bump to check against. The length is increased when new fields are added without disturbing the previous ones.

drm/nouveau/nvkm/subdev/bios/power_budget.c line 125

Quoted 12 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +11 -0
+		entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
+		entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
+		entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
+	} else {
+		entry->min_w = 0;
+		entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
+		entry->avg_w = entry->max_w;
+	}
+
+	return 0;
+}

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#6 lore raw

Re: [PATCH v3 1/3] nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive

2016-11-30 23:48 GMT+01:00 Ben Skeggs <[email protected]>:

Quoted 174 lines · Karol Herbst
Quoted 173 lines

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+     u32 min_w;
+     u32 avg_w;
+     u32 max_w;
+};
+
+struct nvbios_power_budget {
+     u32 offset;
+     u8  hlen;
+     u8  elen;
+     u8  ecount;
+     u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +109 -0
new file mode 100644
index 00000000..04c69095
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst
+ */
+#include <subdev/bios.h>
+#include <subdev/bios/bit.h>
+#include <subdev/bios/power_budget.h>
+
+static u32
+nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
+                       u8 *len)
+{
+     struct bit_entry bit_P;
+     u32 power_budget;
+
+     if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
+         bit_P.length < 0x2c)
+             return 0;
+
+     power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
+     if (!power_budget)
+             return 0;
+
+     *ver = nvbios_rd08(bios, power_budget);
+     switch (*ver) {
+     case 0x10:
+     case 0x20:
+     case 0x30:
+             *hdr = nvbios_rd08(bios, power_budget + 0x1);
+             *len = nvbios_rd08(bios, power_budget + 0x2);
+             *cnt = nvbios_rd08(bios, power_budget + 0x3);
+             return power_budget;
+     default:
+             break;
+     }
+
+     return 0;
+}
+
+int
+nvbios_power_budget_header(struct nvkm_bios *bios,
+                           struct nvbios_power_budget *budget)
+{
+     struct nvkm_subdev *subdev = &bios->subdev;
+     u8 ver, hdr, cnt, len, cap_entry;
+     u32 header;
+
+     if (!bios || !budget)
+             return -EINVAL;
+
+     header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
+     if (!header || !cnt)
+             return -ENODEV;
+
+     budget->offset = header;
+     budget->hlen = hdr;
+     budget->elen = len;
+     budget->ecount = cnt;
+
+     switch (ver) {
+     case 0x20:
+             cap_entry = nvbios_rd08(bios, header + 0x9);
+             break;
+     case 0x30:
+             cap_entry = nvbios_rd08(bios, header + 0xa);
+             break;
+     default:
+             cap_entry = 0xff;
+     }
+
+     if (cap_entry >= cnt && cap_entry != 0xff) {
+             nvkm_warn(subdev,
+                       "invalid cap_entry in power budget table found\n");
+             budget->cap_entry = 0xff;
+             return -EINVAL;
+     }
+
+     budget->cap_entry = cap_entry;
+     return 0;
+}
+
+int
+nvbios_power_budget_entry(struct nvkm_bios *bios,
+                          struct nvbios_power_budget *budget,
+                          u8 idx, struct nvbios_power_budget_entry *entry)
+{
+     u32 entry_offset;
+
+     if (!bios || !budget || !budget->offset || idx >= budget->ecount
+             || !entry)
Quoted 9 lines

drm/nouveau/nvkm/subdev/bios/power_budget.c line 114

Quoted 5 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +5 -0
+             return -EINVAL;
+
+     entry_offset = budget->offset + budget->hlen + idx * budget->elen;
+
+     if (budget->elen >= 0xe) {

I question this check. Generally, when the layout of existing fields changes, there's a version bump to check against. The length is increased when new fields are added without disturbing the previous ones.

and I say this doesn't hold up with this table, because this one is stupid :p

found lengths:

version 10:
0x0c, 0x1d

version 20:
0x0d, 0x1f

version 30:
0x1f, 0x22

Quoted 10 lines
Quoted 10 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +10 -0
+             entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
+     } else {
+             entry->min_w = 0;
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = entry->max_w;
+     }
+
+     return 0;
Quoted 9 lines

drm/nouveau/nvkm/subdev/bios/power_budget.c line 125

Quoted 2 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +1 -0
+}

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#8 lore raw

Re: [PATCH v3 1/3] nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive
Quoted 189 lines · Karol Herbst

2016-11-30 23:48 GMT+01:00 Ben Skeggs <[email protected]>:

Quoted 174 lines · Karol Herbst
Quoted 173 lines

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+     u32 min_w;
+     u32 avg_w;
+     u32 max_w;
+};
+
+struct nvbios_power_budget {
+     u32 offset;
+     u8  hlen;
+     u8  elen;
+     u8  ecount;
+     u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +109 -0
new file mode 100644
index 00000000..04c69095
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst
+ */
+#include <subdev/bios.h>
+#include <subdev/bios/bit.h>
+#include <subdev/bios/power_budget.h>
+
+static u32
+nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
+                       u8 *len)
+{
+     struct bit_entry bit_P;
+     u32 power_budget;
+
+     if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
+         bit_P.length < 0x2c)
+             return 0;
+
+     power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
+     if (!power_budget)
+             return 0;
+
+     *ver = nvbios_rd08(bios, power_budget);
+     switch (*ver) {
+     case 0x10:
+     case 0x20:
+     case 0x30:
+             *hdr = nvbios_rd08(bios, power_budget + 0x1);
+             *len = nvbios_rd08(bios, power_budget + 0x2);
+             *cnt = nvbios_rd08(bios, power_budget + 0x3);
+             return power_budget;
+     default:
+             break;
+     }
+
+     return 0;
+}
+
+int
+nvbios_power_budget_header(struct nvkm_bios *bios,
+                           struct nvbios_power_budget *budget)
+{
+     struct nvkm_subdev *subdev = &bios->subdev;
+     u8 ver, hdr, cnt, len, cap_entry;
+     u32 header;
+
+     if (!bios || !budget)
+             return -EINVAL;
+
+     header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
+     if (!header || !cnt)
+             return -ENODEV;
+
+     budget->offset = header;
+     budget->hlen = hdr;
+     budget->elen = len;
+     budget->ecount = cnt;
+
+     switch (ver) {
+     case 0x20:
+             cap_entry = nvbios_rd08(bios, header + 0x9);
+             break;
+     case 0x30:
+             cap_entry = nvbios_rd08(bios, header + 0xa);
+             break;
+     default:
+             cap_entry = 0xff;
+     }
+
+     if (cap_entry >= cnt && cap_entry != 0xff) {
+             nvkm_warn(subdev,
+                       "invalid cap_entry in power budget table found\n");
+             budget->cap_entry = 0xff;
+             return -EINVAL;
+     }
+
+     budget->cap_entry = cap_entry;
+     return 0;
+}
+
+int
+nvbios_power_budget_entry(struct nvkm_bios *bios,
+                          struct nvbios_power_budget *budget,
+                          u8 idx, struct nvbios_power_budget_entry *entry)
+{
+     u32 entry_offset;
+
+     if (!bios || !budget || !budget->offset || idx >= budget->ecount
+             || !entry)
Quoted 9 lines

drm/nouveau/nvkm/subdev/bios/power_budget.c line 114

Quoted 5 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +5 -0
+             return -EINVAL;
+
+     entry_offset = budget->offset + budget->hlen + idx * budget->elen;
+
+     if (budget->elen >= 0xe) {

I question this check. Generally, when the layout of existing fields changes, there's a version bump to check against. The length is increased when new fields are added without disturbing the previous ones.

and I say this doesn't hold up with this table, because this one is stupid :p

found lengths:

Quoted 8 lines

version 10:
0x0c, 0x1d

version 20:
0x0d, 0x1f

version 30:
0x1f, 0x22

I'm not questioning there's different lengths, I'm questioning whether all <3.0 versions should have the max_w @ +0x2 layout.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +11 -0
+             entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
+     } else {
+             entry->min_w = 0;
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = entry->max_w;
+     }
+
+     return 0;
+}
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

_______________________________________________
Nouveau mailing list [email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#11 lore raw

Re: [PATCH v3 1/3] nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive

2016-11-30 23:56 GMT+01:00 Karol Herbst <[email protected]>:

Quoted 189 lines

2016-11-30 23:48 GMT+01:00 Ben Skeggs <[email protected]>:

Quoted 174 lines · Karol Herbst
Quoted 173 lines

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+     u32 min_w;
+     u32 avg_w;
+     u32 max_w;
+};
+
+struct nvbios_power_budget {
+     u32 offset;
+     u8  hlen;
+     u8  elen;
+     u8  ecount;
+     u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +109 -0
new file mode 100644
index 00000000..04c69095
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst
+ */
+#include <subdev/bios.h>
+#include <subdev/bios/bit.h>
+#include <subdev/bios/power_budget.h>
+
+static u32
+nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
+                       u8 *len)
+{
+     struct bit_entry bit_P;
+     u32 power_budget;
+
+     if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
+         bit_P.length < 0x2c)
+             return 0;
+
+     power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
+     if (!power_budget)
+             return 0;
+
+     *ver = nvbios_rd08(bios, power_budget);
+     switch (*ver) {
+     case 0x10:
+     case 0x20:
+     case 0x30:
+             *hdr = nvbios_rd08(bios, power_budget + 0x1);
+             *len = nvbios_rd08(bios, power_budget + 0x2);
+             *cnt = nvbios_rd08(bios, power_budget + 0x3);
+             return power_budget;
+     default:
+             break;
+     }
+
+     return 0;
+}
+
+int
+nvbios_power_budget_header(struct nvkm_bios *bios,
+                           struct nvbios_power_budget *budget)
+{
+     struct nvkm_subdev *subdev = &bios->subdev;
+     u8 ver, hdr, cnt, len, cap_entry;
+     u32 header;
+
+     if (!bios || !budget)
+             return -EINVAL;
+
+     header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
+     if (!header || !cnt)
+             return -ENODEV;
+
+     budget->offset = header;
+     budget->hlen = hdr;
+     budget->elen = len;
+     budget->ecount = cnt;
+
+     switch (ver) {
+     case 0x20:
+             cap_entry = nvbios_rd08(bios, header + 0x9);
+             break;
+     case 0x30:
+             cap_entry = nvbios_rd08(bios, header + 0xa);
+             break;
+     default:
+             cap_entry = 0xff;
+     }
+
+     if (cap_entry >= cnt && cap_entry != 0xff) {
+             nvkm_warn(subdev,
+                       "invalid cap_entry in power budget table found\n");
+             budget->cap_entry = 0xff;
+             return -EINVAL;
+     }
+
+     budget->cap_entry = cap_entry;
+     return 0;
+}
+
+int
+nvbios_power_budget_entry(struct nvkm_bios *bios,
+                          struct nvbios_power_budget *budget,
+                          u8 idx, struct nvbios_power_budget_entry *entry)
+{
+     u32 entry_offset;
+
+     if (!bios || !budget || !budget->offset || idx >= budget->ecount
+             || !entry)
Quoted 9 lines

drm/nouveau/nvkm/subdev/bios/power_budget.c line 114

Quoted 5 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +5 -0
+             return -EINVAL;
+
+     entry_offset = budget->offset + budget->hlen + idx * budget->elen;
+
+     if (budget->elen >= 0xe) {

I question this check. Generally, when the layout of existing fields changes, there's a version bump to check against. The length is increased when new fields are added without disturbing the previous ones.

and I say this doesn't hold up with this table, because this one is stupid :p

found lengths:

Quoted 9 lines

version 10:
0x0c, 0x1d

version 20:
0x0d, 0x1f

version 30:
0x1f, 0x22

uhh my mistake, you meant the entry size, anyway in the vbios repository are enough examples which show that the length indeed varries and that we can't simply depend on the version here. Also this was taken from nvbios mainly.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +11 -0
+             entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
+     } else {
+             entry->min_w = 0;
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = entry->max_w;
+     }
+
+     return 0;
+}
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#10 lore raw

Re: [PATCH v3 1/3] nvbios/power_budget: Add basic power budget parsing

In reply to a message not in the archive

2016-12-01 0:15 GMT+01:00 Ben Skeggs <[email protected]>:

Quoted 193 lines · Karol Herbst
Quoted 192 lines

2016-11-30 23:48 GMT+01:00 Ben Skeggs <[email protected]>:

Quoted 174 lines · Karol Herbst
Quoted 173 lines

v2: Set entry to 0xff if not found
Add cap entry for ver 0x30 tables
Rework to fix memory leak
v3: More error checks
Simplify check for invalid entries

---
 .../include/nvkm/subdev/bios/power_budget.h        |  25 +++++
 drm/nouveau/nvkm/subdev/bios/Kbuild                |   1 +
 drm/nouveau/nvkm/subdev/bios/power_budget.c        | 125 +++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 drm/nouveau/include/nvkm/subdev/bios/power_budget.h
 create mode 100644 drm/nouveau/nvkm/subdev/bios/power_budget.c
drm/nouveau/include/nvkm/subdev/bios/power_budget.h new file +25 -0
new file mode 100644
index 00000000..89fd4a7a
@@ -0,0 +1,25 @@
+#ifndef __NVBIOS_POWER_BUDGET_H__
+#define __NVBIOS_POWER_BUDGET_H__
+
+#include <nvkm/subdev/bios.h>
+
+struct nvbios_power_budget_entry {
+     u32 min_w;
+     u32 avg_w;
+     u32 max_w;
+};
+
+struct nvbios_power_budget {
+     u32 offset;
+     u8  hlen;
+     u8  elen;
+     u8  ecount;
+     u8  cap_entry;
+};
+
+int nvbios_power_budget_header(struct nvkm_bios *,
+                               struct nvbios_power_budget *);
+int nvbios_power_budget_entry(struct nvkm_bios *, struct nvbios_power_budget *,
+                              u8 idx, struct nvbios_power_budget_entry *);
+
+#endif
drm/nouveau/nvkm/subdev/bios/Kbuild +1 -0
index be57220a..6b4f1e06 100644
@@ -19,6 +19,7 @@ nvkm-y += nvkm/subdev/bios/pcir.o
 nvkm-y += nvkm/subdev/bios/perf.o
 nvkm-y += nvkm/subdev/bios/pll.o
 nvkm-y += nvkm/subdev/bios/pmu.o
+nvkm-y += nvkm/subdev/bios/power_budget.o
 nvkm-y += nvkm/subdev/bios/ramcfg.o
 nvkm-y += nvkm/subdev/bios/rammap.o
 nvkm-y += nvkm/subdev/bios/shadow.o
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +109 -0
new file mode 100644
index 00000000..04c69095
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2016 Karol Herbst
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Karol Herbst
+ */
+#include <subdev/bios.h>
+#include <subdev/bios/bit.h>
+#include <subdev/bios/power_budget.h>
+
+static u32
+nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
+                       u8 *len)
+{
+     struct bit_entry bit_P;
+     u32 power_budget;
+
+     if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
+         bit_P.length < 0x2c)
+             return 0;
+
+     power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
+     if (!power_budget)
+             return 0;
+
+     *ver = nvbios_rd08(bios, power_budget);
+     switch (*ver) {
+     case 0x10:
+     case 0x20:
+     case 0x30:
+             *hdr = nvbios_rd08(bios, power_budget + 0x1);
+             *len = nvbios_rd08(bios, power_budget + 0x2);
+             *cnt = nvbios_rd08(bios, power_budget + 0x3);
+             return power_budget;
+     default:
+             break;
+     }
+
+     return 0;
+}
+
+int
+nvbios_power_budget_header(struct nvkm_bios *bios,
+                           struct nvbios_power_budget *budget)
+{
+     struct nvkm_subdev *subdev = &bios->subdev;
+     u8 ver, hdr, cnt, len, cap_entry;
+     u32 header;
+
+     if (!bios || !budget)
+             return -EINVAL;
+
+     header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
+     if (!header || !cnt)
+             return -ENODEV;
+
+     budget->offset = header;
+     budget->hlen = hdr;
+     budget->elen = len;
+     budget->ecount = cnt;
+
+     switch (ver) {
+     case 0x20:
+             cap_entry = nvbios_rd08(bios, header + 0x9);
+             break;
+     case 0x30:
+             cap_entry = nvbios_rd08(bios, header + 0xa);
+             break;
+     default:
+             cap_entry = 0xff;
+     }
+
+     if (cap_entry >= cnt && cap_entry != 0xff) {
+             nvkm_warn(subdev,
+                       "invalid cap_entry in power budget table found\n");
+             budget->cap_entry = 0xff;
+             return -EINVAL;
+     }
+
+     budget->cap_entry = cap_entry;
+     return 0;
+}
+
+int
+nvbios_power_budget_entry(struct nvkm_bios *bios,
+                          struct nvbios_power_budget *budget,
+                          u8 idx, struct nvbios_power_budget_entry *entry)
+{
+     u32 entry_offset;
+
+     if (!bios || !budget || !budget->offset || idx >= budget->ecount
+             || !entry)
Quoted 9 lines

drm/nouveau/nvkm/subdev/bios/power_budget.c line 114

Quoted 5 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +5 -0
+             return -EINVAL;
+
+     entry_offset = budget->offset + budget->hlen + idx * budget->elen;
+
+     if (budget->elen >= 0xe) {

I question this check. Generally, when the layout of existing fields changes, there's a version bump to check against. The length is increased when new fields are added without disturbing the previous ones.

and I say this doesn't hold up with this table, because this one is stupid :p

found lengths:

version 10:
0x0c, 0x1d

Quoted 8 lines
Quoted 5 lines

version 20:
0x0d, 0x1f

version 30:
0x1f, 0x22

I'm not questioning there's different lengths, I'm questioning whether all <3.0 versions should have the max_w @ +0x2 layout.

ohh I see. Not quite sure, I don't think there is actually a 2.0 table with smaller entries and I didn't really had time to confirm this on 1.0 tables.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
drm/nouveau/nvkm/subdev/bios/power_budget.c new file +11 -0
+             entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
+     } else {
+             entry->min_w = 0;
+             entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
+             entry->avg_w = entry->max_w;
+     }
+
+     return 0;
+}
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 1 line

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau