^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) Rockchip Electronics Co.Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Felix Zeng <felix.zeng@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/devfreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/clk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #ifndef FPGA_PLATFORM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #ifdef CONFIG_PM_DEVFREQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <../drivers/devfreq/governor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "rknpu_drv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "rknpu_mm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "rknpu_reset.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "rknpu_debugger.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define RKNPU_DEBUGGER_ROOT_NAME "rknpu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #if defined(CONFIG_ROCKCHIP_RKNPU_DEBUG_FS) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) defined(CONFIG_ROCKCHIP_RKNPU_PROC_FS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int rknpu_version_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) seq_printf(m, "%s: v%d.%d.%d\n", DRIVER_DESC, DRIVER_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) DRIVER_MINOR, DRIVER_PATCHLEVEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static int rknpu_load_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct rknpu_subcore_data *subcore_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) uint64_t busy_time_total, div_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) seq_puts(m, "NPU load: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (i = 0; i < rknpu_dev->config->num_irqs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) subcore_data = &rknpu_dev->subcore_datas[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (rknpu_dev->config->num_irqs > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) seq_printf(m, " Core%d: ", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spin_lock_irqsave(&rknpu_dev->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) busy_time_total = subcore_data->timer.busy_time_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_unlock_irqrestore(&rknpu_dev->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) div_value = (RKNPU_LOAD_INTERVAL / 100000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) do_div(busy_time_total, div_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) load = busy_time_total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (rknpu_dev->config->num_irqs > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) seq_printf(m, "%2.d%%,", load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) seq_printf(m, "%2.d%%", load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) seq_puts(m, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int rknpu_power_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (atomic_read(&rknpu_dev->power_refcount) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) seq_puts(m, "on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) seq_puts(m, "off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static ssize_t rknpu_power_set(struct file *file, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) size_t len, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct seq_file *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct rknpu_debugger_node *node = priv->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) char buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (len > sizeof(buf) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (copy_from_user(buf, ubuf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (strcmp(buf, "on") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) atomic_inc(&rknpu_dev->cmdline_power_refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rknpu_power_get(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) LOG_INFO("rknpu power is on!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) } else if (strcmp(buf, "off") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (atomic_read(&rknpu_dev->power_refcount) > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) atomic_dec_if_positive(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) &rknpu_dev->cmdline_power_refcount) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) atomic_sub(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) atomic_read(&rknpu_dev->cmdline_power_refcount),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) &rknpu_dev->power_refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) atomic_set(&rknpu_dev->cmdline_power_refcount, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) rknpu_power_put(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (atomic_read(&rknpu_dev->power_refcount) <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) LOG_INFO("rknpu power is off!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) LOG_ERROR("rknpu power node params is invalid!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int rknpu_power_put_delay_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) seq_printf(m, "%lu\n", rknpu_dev->power_put_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static ssize_t rknpu_power_put_delay_set(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const char __user *ubuf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct seq_file *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct rknpu_debugger_node *node = priv->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) char buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long power_put_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (len > sizeof(buf) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (copy_from_user(buf, ubuf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = kstrtoul(buf, 10, &power_put_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) LOG_ERROR("failed to parse power put delay string: %s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) rknpu_dev->power_put_delay = power_put_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) LOG_INFO("set rknpu power put delay time %lums\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rknpu_dev->power_put_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int rknpu_freq_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned long current_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) rknpu_power_get(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) current_freq = clk_get_rate(rknpu_dev->clks[0].clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) rknpu_power_put(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) seq_printf(m, "%lu\n", current_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #ifdef CONFIG_PM_DEVFREQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static ssize_t rknpu_freq_set(struct file *file, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) size_t len, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct seq_file *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct rknpu_debugger_node *node = priv->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned long current_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) char buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned long freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (len > sizeof(buf) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (copy_from_user(buf, ubuf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = kstrtoul(buf, 10, &freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) LOG_ERROR("failed to parse freq string: %s\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (!rknpu_dev->devfreq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) rknpu_power_get(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) current_freq = clk_get_rate(rknpu_dev->clks[0].clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (freq != current_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) rknpu_dev->ondemand_freq = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) mutex_lock(&rknpu_dev->devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) update_devfreq(rknpu_dev->devfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) mutex_unlock(&rknpu_dev->devfreq->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) rknpu_power_put(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static ssize_t rknpu_freq_set(struct file *file, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) size_t len, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int rknpu_volt_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) unsigned long current_volt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) current_volt = regulator_get_voltage(rknpu_dev->vdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) seq_printf(m, "%lu\n", current_volt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static int rknpu_reset_show(struct seq_file *m, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct rknpu_debugger_node *node = m->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!rknpu_dev->bypass_soft_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) seq_puts(m, "on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) seq_puts(m, "off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static ssize_t rknpu_reset_set(struct file *file, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) size_t len, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct seq_file *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct rknpu_debugger_node *node = priv->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct rknpu_debugger *debugger = node->debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct rknpu_device *rknpu_dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) container_of(debugger, struct rknpu_device, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) char buf[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (len > sizeof(buf) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (copy_from_user(buf, ubuf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (strcmp(buf, "1") == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) atomic_read(&rknpu_dev->power_refcount) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rknpu_soft_reset(rknpu_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) else if (strcmp(buf, "on") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) rknpu_dev->bypass_soft_reset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) else if (strcmp(buf, "off") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) rknpu_dev->bypass_soft_reset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static struct rknpu_debugger_list rknpu_debugger_root_list[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) { "version", rknpu_version_show, NULL, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) { "load", rknpu_load_show, NULL, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) { "power", rknpu_power_show, rknpu_power_set, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) { "freq", rknpu_freq_show, rknpu_freq_set, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) { "volt", rknpu_volt_show, NULL, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) { "delayms", rknpu_power_put_delay_show, rknpu_power_put_delay_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) { "reset", rknpu_reset_show, rknpu_reset_set, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #ifdef CONFIG_ROCKCHIP_RKNPU_SRAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) { "mm", rknpu_mm_dump, NULL, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static ssize_t rknpu_debugger_write(struct file *file, const char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) size_t len, loff_t *offp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct seq_file *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct rknpu_debugger_node *node = priv->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (node->info_ent->write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return node->info_ent->write(file, ubuf, len, offp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int rknpu_debugfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct rknpu_debugger_node *node = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return single_open(file, node->info_ent->show, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static const struct file_operations rknpu_debugfs_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .open = rknpu_debugfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .release = single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .write = rknpu_debugger_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #endif /* #if defined(CONFIG_ROCKCHIP_RKNPU_DEBUG_FS) || defined(CONFIG_ROCKCHIP_RKNPU_PROC_FS) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #ifdef CONFIG_ROCKCHIP_RKNPU_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int rknpu_debugfs_remove_files(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct rknpu_debugger_node *pos, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct list_head *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) mutex_lock(&debugger->debugfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* Delete debugfs entry list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) entry_list = &debugger->debugfs_entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) list_for_each_entry_safe(pos, q, entry_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (pos->dent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) list_del(&pos->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) kfree(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* Delete all debugfs node in this directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) debugfs_remove_recursive(debugger->debugfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) debugger->debugfs_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) mutex_unlock(&debugger->debugfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int rknpu_debugfs_create_files(const struct rknpu_debugger_list *files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int count, struct dentry *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct dentry *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct rknpu_debugger_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) tmp = kmalloc(sizeof(struct rknpu_debugger_node), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (tmp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) LOG_ERROR(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) "Cannot alloc node path /sys/kernel/debug/%pd/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) root, files[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto MALLOC_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) tmp->info_ent = &files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) tmp->debugger = debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) root, tmp, &rknpu_debugfs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (!ent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) LOG_ERROR("Cannot create /sys/kernel/debug/%pd/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) root, files[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) goto CREATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) tmp->dent = ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) mutex_lock(&debugger->debugfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) list_add_tail(&tmp->list, &debugger->debugfs_entry_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) mutex_unlock(&debugger->debugfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) CREATE_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) MALLOC_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) rknpu_debugfs_remove_files(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int rknpu_debugfs_remove(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) rknpu_debugfs_remove_files(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int rknpu_debugfs_init(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) debugger->debugfs_dir =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) debugfs_create_dir(RKNPU_DEBUGGER_ROOT_NAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (IS_ERR_OR_NULL(debugger->debugfs_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) LOG_ERROR("failed on mkdir /sys/kernel/debug/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) RKNPU_DEBUGGER_ROOT_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) debugger->debugfs_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ret = rknpu_debugfs_create_files(rknpu_debugger_root_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ARRAY_SIZE(rknpu_debugger_root_list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) debugger->debugfs_dir, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) LOG_ERROR(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) "Could not install rknpu_debugger_root_list debugfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) goto CREATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) CREATE_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) rknpu_debugfs_remove(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) #endif /* #ifdef CONFIG_ROCKCHIP_RKNPU_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) #ifdef CONFIG_ROCKCHIP_RKNPU_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static int rknpu_procfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) struct rknpu_debugger_node *node = PDE_DATA(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return single_open(file, node->info_ent->show, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static const struct proc_ops rknpu_procfs_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) .proc_open = rknpu_procfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) .proc_release = single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) .proc_write = rknpu_debugger_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) static int rknpu_procfs_remove_files(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct rknpu_debugger_node *pos, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct list_head *entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) mutex_lock(&debugger->procfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /* Delete procfs entry list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) entry_list = &debugger->procfs_entry_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) list_for_each_entry_safe(pos, q, entry_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (pos->pent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) list_del(&pos->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) kfree(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /* Delete all procfs node in this directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) proc_remove(debugger->procfs_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) debugger->procfs_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) mutex_unlock(&debugger->procfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static int rknpu_procfs_create_files(const struct rknpu_debugger_list *files,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) int count, struct proc_dir_entry *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) struct proc_dir_entry *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct rknpu_debugger_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) tmp = kmalloc(sizeof(struct rknpu_debugger_node), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (tmp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) LOG_ERROR("Cannot alloc node path for /proc/%s/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) RKNPU_DEBUGGER_ROOT_NAME, files[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) goto MALLOC_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) tmp->info_ent = &files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) tmp->debugger = debugger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) ent = proc_create_data(files[i].name, S_IFREG | S_IRUGO, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) &rknpu_procfs_fops, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (!ent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) LOG_ERROR("Cannot create /proc/%s/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) RKNPU_DEBUGGER_ROOT_NAME, files[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) goto CREATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) tmp->pent = ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) mutex_lock(&debugger->procfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) list_add_tail(&tmp->list, &debugger->procfs_entry_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) mutex_unlock(&debugger->procfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) CREATE_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) MALLOC_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) rknpu_procfs_remove_files(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static int rknpu_procfs_remove(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) rknpu_procfs_remove_files(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static int rknpu_procfs_init(struct rknpu_debugger *debugger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) debugger->procfs_dir = proc_mkdir(RKNPU_DEBUGGER_ROOT_NAME, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (IS_ERR_OR_NULL(debugger->procfs_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) pr_err("failed on mkdir /proc/%s\n", RKNPU_DEBUGGER_ROOT_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) debugger->procfs_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = rknpu_procfs_create_files(rknpu_debugger_root_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ARRAY_SIZE(rknpu_debugger_root_list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) debugger->procfs_dir, debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) pr_err("Could not install rknpu_debugger_root_list procfs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto CREATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) CREATE_FAIL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) rknpu_procfs_remove(debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) #endif /* #ifdef CONFIG_ROCKCHIP_RKNPU_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) int rknpu_debugger_init(struct rknpu_device *rknpu_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) #ifdef CONFIG_ROCKCHIP_RKNPU_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) mutex_init(&rknpu_dev->debugger.debugfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) INIT_LIST_HEAD(&rknpu_dev->debugger.debugfs_entry_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) rknpu_debugfs_init(&rknpu_dev->debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) #ifdef CONFIG_ROCKCHIP_RKNPU_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) mutex_init(&rknpu_dev->debugger.procfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) INIT_LIST_HEAD(&rknpu_dev->debugger.procfs_entry_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) rknpu_procfs_init(&rknpu_dev->debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int rknpu_debugger_remove(struct rknpu_device *rknpu_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) #ifdef CONFIG_ROCKCHIP_RKNPU_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) rknpu_debugfs_remove(&rknpu_dev->debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) #ifdef CONFIG_ROCKCHIP_RKNPU_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) rknpu_procfs_remove(&rknpu_dev->debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }