From b10824bc07beb685adc63d412ec70683240d5643 Mon Sep 17 00:00:00 2001 From: zhangzhaopeng Date: Fri, 21 Feb 2025 17:40:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=92=8C=E8=AE=BE=E7=BD=AE=E5=94=A4=E9=86=92=E6=BA=90?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../char/mem_operation/mem_operation_drv.c | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) mode change 100644 => 100755 lichee/linux-4.9/drivers/char/mem_operation/mem_operation_drv.c diff --git a/lichee/linux-4.9/drivers/char/mem_operation/mem_operation_drv.c b/lichee/linux-4.9/drivers/char/mem_operation/mem_operation_drv.c old mode 100644 new mode 100755 index d3c3bdd8b..d06fffad2 --- a/lichee/linux-4.9/drivers/char/mem_operation/mem_operation_drv.c +++ b/lichee/linux-4.9/drivers/char/mem_operation/mem_operation_drv.c @@ -20,9 +20,13 @@ #include #include #include +#include #define WEIGHT_ADDR 0x4307F000 // 权重加载的内存地址处 -#define MEM_ACCESS_RELEASE_PHYS_ADDR _IOR('M', 0, unsigned int) // 自定义ioctl命令,用于释放预留内存 +#define M_PAGE_SIZE (4 * 1024) // 内存对齐,应该是4K +#define MEM_ACCESS_RELEASE_PHYS_ADDR _IOR('M', 0, unsigned int) +#define MEM_ACCESS_GET_WKSRC_SRC _IOW('M', 1, unsigned int) +#define MEM_ACCESS_SET_WKSRC_SRC _IOR('M', 2, unsigned int) static int major = 0; static struct class *mem_operation_class; @@ -73,9 +77,14 @@ static int mem_operation_drv_mmap(struct file *file, struct vm_area_struct *vma) return 0; } +typedef struct { + unsigned char readData[sizeof(int)]; +} wakeup_src_t; + + static long mem_operation_drv_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { - int ret = 0; + wakeup_src_t wakeupsrc = {.readData[0] = 0}; switch (cmd) { case MEM_ACCESS_RELEASE_PHYS_ADDR: { @@ -92,12 +101,59 @@ static long mem_operation_drv_ioctl(struct file *file, unsigned int cmd, unsigne printk("release phy addr: 0x%x, size: %d\n", WEIGHT_ADDR, mem_size); break; } + case MEM_ACCESS_GET_WKSRC_SRC: + { + void __iomem *vaddr = NULL; + + // vaddr = ioremap(WEIGHT_ADDR, sizeof(wakeupsrc)); + // vaddr = ioremap_nocache(PHYS_ADDR, SIZE); + vaddr = memremap(WEIGHT_ADDR, M_PAGE_SIZE, MEMREMAP_WB); + if (!vaddr) + { + return -EINVAL; + } + + wakeupsrc.readData[0] = readb(vaddr); + // memcpy_fromio(buffer, vaddr, SIZE); + // copy_to_user(arg, vaddr, SIZE) ? + // printk(KERN_EMERG "give wake up source:%d\n", wakeupsrc.readData[0]); + + if (copy_to_user(arg, &wakeupsrc, sizeof(wakeupsrc))) + { + // iounmap(vaddr); + memunmap(vaddr); + return -EINVAL; + } + // iounmap(vaddr); + memunmap(vaddr); + break; + } + case MEM_ACCESS_SET_WKSRC_SRC: + { + void __iomem *vaddr = NULL; + + if (copy_from_user(&wakeupsrc, (void __user *)arg, sizeof(wakeupsrc))) + { + return -EFAULT; + } + vaddr = memremap(WEIGHT_ADDR, M_PAGE_SIZE, MEMREMAP_WB); + if (!vaddr) + { + return -EINVAL; + } + writeb(wakeupsrc.readData[0], vaddr); + + memunmap(vaddr); + + // printk(KERN_EMERG "set wake up source:%d\n", wakeupsrc.readData[0]); + break; + } default: - ret = -EINVAL; + return -EINVAL; break; } - return ret; + return 0; } static struct file_operations mem_operation_drv = {