diff --git a/src/hal/novatek.c b/src/hal/novatek.c index 26379d5..27a4d1a 100644 --- a/src/hal/novatek.c +++ b/src/hal/novatek.c @@ -122,25 +122,59 @@ unsigned long novatek_totalmem(unsigned long *media_mem) { return *media_mem + kernel_mem(); } -/* The vendor nvt_adc driver's thermal zone. It reports whole degrees, not the - * millidegrees the sysfs ABI promises, so the value is taken as it stands. */ +/* A thermal zone in either unit. The vendor nvt_adc driver reports whole + * degrees; OpenIPC's reports the millidegrees the sysfs ABI promises + * (OpenIPC/linux#323). A die reads -40..125 degC, so whole degrees stay inside + * [-100, 200], and nvt_adc's millidegrees -- 30000 + k * 1319, or k * 1300 on + * NA51055 -- land in there only at 100: an NA51055 at 0.1 degC. */ +static float zone_celsius(const char *path) { + char buf[16], *end; + if (!line_from_file(path, "(-?[0-9]+)", buf, sizeof(buf))) + return NAN; + const long v = strtol(buf, &end, 10); + if (end == buf) + return NAN; + return v < -100 || v > 200 ? v / 1000.0f : v; +} + float novatek_get_temp() { - char buf[16]; - if (line_from_file("/sys/class/thermal/thermal_zone0/temp", "(.+)", buf, - sizeof(buf))) - return strtof(buf, NULL); - return NAN; + return zone_celsius("/sys/class/thermal/thermal_zone0/temp"); +} + +/* nvt_adc's own zone, by type rather than by number. Where the kernel has + * it, the ADC and the eFuse are the kernel's: it read the trim once, at + * probe, under the semaphore that also orders the encoder's eFuse check + * (kdrv_h26x calls it from kdrv_videoenc_set), and the register path below + * cannot take that semaphore. So with the zone there, no register is read. + * The scan steps over missing numbers: a driver that unregisters its zone + * leaves a gap. */ +static bool nvt_zone_temp(float *t) { + char path[64], type[32]; + for (int i = 0; i < 16; i++) { + snprintf(path, sizeof(path), "/sys/class/thermal/thermal_zone%d/type", + i); + if (!line_from_file(path, "([a-z0-9_]+)", type, sizeof(type))) + continue; + if (!strcmp(type, "nvt_thermal")) { + snprintf(path, sizeof(path), + "/sys/class/thermal/thermal_zone%d/temp", i); + *t = zone_celsius(path); + return true; + } + } + return false; } /* The die thermometer of NA51055, NA51084 and NA51089 -- one SAR ADC, clock * gate and eFuse block at the same addresses on all three, per the SDK's * plat-na51055 (which serves NA51055 and NA51084) and plat-na51089 -- is - * channel 3 of the ADC, averaged by the ADC into its own register. OpenIPC's - * kernel builds no nvt_adc driver, so the ADC is found gated and idle, and is - * started here the way the RTOS side's thermal_channel_init() does: clock on, - * channel 3 continuous, converter on. Both are left running, as the vendor - * driver leaves them; turning a clock off is how a probe takes a peripheral - * away from its owner (OpenIPC/firmware#2439). */ + * channel 3 of the ADC, averaged by the ADC into its own register. A kernel + * without nvt_adc (OpenIPC's, until OpenIPC/firmware#2950) leaves the ADC + * gated and idle, so it is started here the way the RTOS side's + * thermal_channel_init() does: clock on, channel 3 continuous, converter on. + * Both are left running, as the vendor driver leaves them; turning a clock off + * is how a probe takes a peripheral away from its owner + * (OpenIPC/firmware#2439). */ #define NVT_CG_CLK_EN1 0xF0020074 #define NVT_CG_ADC (1u << 13) #define NVT_ADC_CTRL 0xF0260000 @@ -221,19 +255,33 @@ static bool nvt_efuse_row(uint32_t row, uint32_t *data) { * * The row is read twice and taken only if both agree. Nothing here can lock * out the kernel or a second ipctool, and a sequence one of them interleaved - * with comes back as a different word, which costs the calibration and - * nothing else. */ + * with comes back as a different word, which costs us the calibration. It can + * also hand the kernel's reader our row, which is why the result is kept: a + * fuse does not change, and a libipchw consumer such as a metrics exporter + * may ask for the temperature every few seconds. A read that did not complete + * is not kept, so the next call tries again rather than living with the + * default. */ +static int nvt_trim = -1; + static int nvt_thermal_trim() { + if (nvt_trim >= 0) + return nvt_trim; + const int dflt = chip_generation == CHIP_NA51055 ? NA51055_DEFAULT_TRIM + : NVT_DEFAULT_TRIM; uint32_t row, again; - if (nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &row) && - nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &again) && row == again) - for (int half = 0; half < 2; half++) { - const uint32_t w = (row >> (16 * half)) & 0xffff; - if ((w & 0xc000) == 0x8000) - return w & 0x3fff; + if (!nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &row) || + !nvt_efuse_row(NVT_THERMAL_TRIM_ROW, &again) || row != again) + return dflt; + int trim = dflt; + for (int half = 0; half < 2; half++) { + const uint32_t w = (row >> (16 * half)) & 0xffff; + if ((w & 0xc000) == 0x8000) { + trim = w & 0x3fff; + break; } - return chip_generation == CHIP_NA51055 ? NA51055_DEFAULT_TRIM - : NVT_DEFAULT_TRIM; + } + nvt_trim = trim; /* stored once, never half-decided */ + return trim; } static float nvt_read_temp() { @@ -269,9 +317,12 @@ static float nvt_read_temp() { return ((int)code - nvt_thermal_trim()) * slope + 30; } -/* A kernel that withholds /dev/mem may still carry the vendor driver. */ static float nvt_get_temp() { - const float t = nvt_read_temp(); + float t; + if (nvt_zone_temp(&t)) + return t; + t = nvt_read_temp(); + /* A kernel that withholds /dev/mem may still have a zone of its own. */ return isnan(t) ? novatek_get_temp() : t; } diff --git a/src/hal/novatek_test.c b/src/hal/novatek_test.c index 08f294f..21aa446 100644 --- a/src/hal/novatek_test.c +++ b/src/hal/novatek_test.c @@ -43,10 +43,13 @@ static struct { uint32_t second_read; /* if set, what an interleaved second read sees */ } M; -static int efuse_writes, adc_writes, cg_writes; +static int efuse_writes, adc_writes, cg_writes, mmio_calls; static const char *illegal; -static bool mmio_fails; /* /dev/mem withheld */ -static const char *dt_model, *sysfs; /* line_from_file's answers */ +static bool mmio_fails; /* /dev/mem withheld */ +static const char *dt_model; /* line_from_file's answers */ +static struct { + const char *type, *temp; /* NULL type: no such zone */ +} zone[4]; static void fresh(void) { memset(&M, 0, sizeof M); @@ -56,11 +59,13 @@ static void fresh(void) { M.efuse_sts = 0x0000000C; M.fuse[6] = 0x000080E4; M.code = 265; - efuse_writes = adc_writes = cg_writes = 0; + efuse_writes = adc_writes = cg_writes = mmio_calls = 0; illegal = NULL; mmio_fails = false; - dt_model = sysfs = NULL; + dt_model = NULL; + memset(zone, 0, sizeof zone); chip_generation = CHIP_NA51089; + nvt_trim = -1; } static bool converting(void) { @@ -77,6 +82,7 @@ static void complete_read(void) { } bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { + mmio_calls++; if (mmio_fails) return false; if (op == OP_READ) { @@ -162,10 +168,14 @@ bool mem_reg(uint32_t addr, uint32_t *data, enum REG_OPS op) { bool line_from_file(const char *filename, const char *re, char *buf, size_t n) { (void)re; const char *ans = NULL; + int i; + char leaf[8]; if (!strcmp(filename, "/proc/device-tree/model")) ans = dt_model; - else if (!strcmp(filename, "/sys/class/thermal/thermal_zone0/temp")) - ans = sysfs; + else if (sscanf(filename, "/sys/class/thermal/thermal_zone%d/%7s", &i, + leaf) == 2 && + i >= 0 && i < 4 && zone[i].type) + ans = !strcmp(leaf, "type") ? zone[i].type : zone[i].temp; if (!ans) return false; snprintf(buf, n, "%s", ans); @@ -205,11 +215,12 @@ int main(void) { CHECK(efuse_closed()); CHECK(illegal == NULL); - /* A second read touches neither the clock nor the ADC. */ - cg_writes = adc_writes = 0; + /* A second read touches neither the clock nor the ADC, and the trim is + * not read again: every sequence is a window on the kernel's readers. */ + cg_writes = adc_writes = efuse_writes = 0; t = hal_temperature(); CHECK(near(t, 265, 228)); - CHECK(cg_writes == 0 && adc_writes == 0); + CHECK(cg_writes == 0 && adc_writes == 0 && efuse_writes == 0); /* An ADC someone else runs is read, not reconfigured: the vendor driver * leaves clkdiv 1 and all four channels continuous. */ @@ -246,13 +257,17 @@ int main(void) { CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); /* The kernel mid-sequence: the controller is left alone entirely, and - * the reading falls back to the default trim rather than failing. */ + * the reading falls back to the default trim rather than failing -- for + * this call only. Once the kernel is done, the fused trim is read. */ fresh(); M.efuse_ctrl = 1; M.efuse_sts |= 1; t = nvt_get_temp(); CHECK(near(t, 265, NVT_DEFAULT_TRIM)); CHECK(efuse_writes == 0); + M.efuse_ctrl = 0; + M.efuse_sts &= ~1u; + CHECK(near(nvt_get_temp(), 265, 228)); /* A read-done bit someone left standing, with their row still in DATA: * that word is not row 6, and must not be taken for it. */ @@ -268,14 +283,74 @@ int main(void) { M.second_read = 0x000080F0; CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); CHECK(M.reads_of_row6 == 2); + M.second_read = 0; + CHECK(near(nvt_get_temp(), 265, 228)); + + /* An unfused die is an answer, not a failure: kept like a fused one. */ + fresh(); + M.fuse[6] = 0; + CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); + efuse_writes = 0; + CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); + CHECK(efuse_writes == 0); + + /* The kernel carries nvt_adc (OpenIPC/firmware#2950, with the units of + * OpenIPC/linux#323): its zone is read, and no register is touched. */ + fresh(); + zone[0].type = "nvt_thermal"; + zone[0].temp = "47147"; + CHECK(fabsf(nvt_get_temp() - 47.147f) < 0.001f); + CHECK(mmio_calls == 0); + + /* The vendor's own kernel, in whole degrees. */ + zone[0].temp = "52"; + CHECK(nvt_get_temp() == 52.0f); + CHECK(mmio_calls == 0); + + /* Below zero either way: nvt_adc's millidegrees nearest 0 on NA51089 + * are -337 and 982, both outside the whole-degree window. */ + zone[0].temp = "-337"; + CHECK(fabsf(nvt_get_temp() + 0.337f) < 0.001f); + zone[0].temp = "-12"; + CHECK(nvt_get_temp() == -12.0f); + + /* A gap in the numbering: a driver that unregistered its zone. */ + fresh(); + zone[1].type = "nvt_thermal"; + zone[1].temp = "45828"; + CHECK(fabsf(nvt_get_temp() - 45.828f) < 0.001f); + CHECK(mmio_calls == 0); + + /* Found by type, not by number. */ + fresh(); + zone[0].type = "cpu_thermal"; + zone[0].temp = "99000"; + zone[1].type = "nvt_thermal"; + zone[1].temp = "45828"; + CHECK(fabsf(nvt_get_temp() - 45.828f) < 0.001f); + CHECK(mmio_calls == 0); + + /* The zone is there but will not read: no reading, and still no + * register -- the ADC and the eFuse are the kernel's. */ + fresh(); + zone[0].type = "nvt_thermal"; + CHECK(isnan(nvt_get_temp())); + CHECK(mmio_calls == 0); - /* /dev/mem withheld: the vendor driver's thermal zone, if there is one, - * still answers -- and with none, there is no reading. */ + /* Some other zone only: the registers, as with none. */ + fresh(); + zone[0].type = "cpu_thermal"; + zone[0].temp = "99000"; + CHECK(near(nvt_get_temp(), 265, 228)); + + /* /dev/mem withheld and no nvt_adc: some other zone if there is one, + * else no reading. */ fresh(); mmio_fails = true; - sysfs = "66"; + zone[0].type = "cpu_thermal"; + zone[0].temp = "66000"; CHECK(nvt_get_temp() == 66.0f); - sysfs = NULL; + zone[0].type = NULL; CHECK(isnan(nvt_get_temp())); /* A kernel without nvt_pinmux: the device tree names the die, and that @@ -305,6 +380,7 @@ int main(void) { CHECK(hal_temperature == nvt_get_temp); CHECK(near(hal_temperature(), 265, 228)); M.fuse[6] = 0; + nvt_trim = -1; /* another die, so another process */ CHECK(near(nvt_get_temp(), 265, NVT_DEFAULT_TRIM)); /* NA51055 has its own slope and default: 1.3 degC per code, trim 259. */ @@ -315,6 +391,7 @@ int main(void) { CHECK(hal_temperature == nvt_get_temp); CHECK(near55(hal_temperature(), 265, 228)); M.fuse[6] = 0; + nvt_trim = -1; CHECK(near55(nvt_get_temp(), 265, NA51055_DEFAULT_TRIM)); CHECK(illegal == NULL); @@ -342,6 +419,16 @@ int main(void) { CHECK(hal_temperature != nvt_get_temp); } + /* Their zone is read in whichever unit its driver uses. */ + fresh(); + zone[0].type = "thermal"; + zone[0].temp = "66"; + CHECK(novatek_get_temp() == 66.0f); + zone[0].temp = "45000"; + CHECK(novatek_get_temp() == 45.0f); + zone[0].type = NULL; + CHECK(isnan(novatek_get_temp())); + if (failures) fprintf(stderr, "novatek_test: %d failure(s)\n", failures); else