Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ACPI AML interfacing support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Authors: Lv Zheng <lv.zheng@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) "ACPI: AML: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/circ_buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define ACPI_AML_BUF_ALIGN	(sizeof (acpi_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define ACPI_AML_BUF_SIZE	PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define circ_count(circ) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	(CIRC_CNT((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define circ_count_to_end(circ) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	(CIRC_CNT_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define circ_space(circ) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	(CIRC_SPACE((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define circ_space_to_end(circ) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	(CIRC_SPACE_TO_END((circ)->head, (circ)->tail, ACPI_AML_BUF_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define ACPI_AML_OPENED		0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define ACPI_AML_CLOSED		0x0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define ACPI_AML_IN_USER	0x0004 /* user space is writing cmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define ACPI_AML_IN_KERN	0x0008 /* kernel space is reading cmd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define ACPI_AML_OUT_USER	0x0010 /* user space is reading log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ACPI_AML_OUT_KERN	0x0020 /* kernel space is writing log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define ACPI_AML_USER		(ACPI_AML_IN_USER | ACPI_AML_OUT_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define ACPI_AML_KERN		(ACPI_AML_IN_KERN | ACPI_AML_OUT_KERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define ACPI_AML_BUSY		(ACPI_AML_USER | ACPI_AML_KERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define ACPI_AML_OPEN		(ACPI_AML_OPENED | ACPI_AML_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct acpi_aml_io {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	wait_queue_head_t wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned long users;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct task_struct *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	char out_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct circ_buf out_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char in_buf[ACPI_AML_BUF_SIZE] __aligned(ACPI_AML_BUF_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct circ_buf in_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	acpi_osd_exec_callback function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	void *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned long usages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static struct acpi_aml_io acpi_aml_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static bool acpi_aml_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct file *acpi_aml_active_reader;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static struct dentry *acpi_aml_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static inline bool __acpi_aml_running(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return acpi_aml_io.thread ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static inline bool __acpi_aml_access_ok(unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	 * The debugger interface is in opened state (OPENED && !CLOSED),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 * then it is allowed to access the debugger buffers from either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * user space or the kernel space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * In addition, for the kernel space, only the debugger thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * (thread ID matched) is allowed to access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (!(acpi_aml_io.flags & ACPI_AML_OPENED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	    (acpi_aml_io.flags & ACPI_AML_CLOSED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	    !__acpi_aml_running())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if ((flag & ACPI_AML_KERN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	    current != acpi_aml_io.thread)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static inline bool __acpi_aml_readable(struct circ_buf *circ, unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * Another read is not in progress and there is data in buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * available for read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (!(acpi_aml_io.flags & flag) && circ_count(circ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline bool __acpi_aml_writable(struct circ_buf *circ, unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * Another write is not in progress and there is buffer space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * available for write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!(acpi_aml_io.flags & flag) && circ_space(circ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static inline bool __acpi_aml_busy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (acpi_aml_io.flags & ACPI_AML_BUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline bool __acpi_aml_opened(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (acpi_aml_io.flags & ACPI_AML_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline bool __acpi_aml_used(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return acpi_aml_io.usages ? true : false;
^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 inline bool acpi_aml_running(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ret = __acpi_aml_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static bool acpi_aml_busy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	ret = __acpi_aml_busy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static bool acpi_aml_used(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * The usage count is prepared to avoid race conditions between the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * starts and the stops of the debugger thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = __acpi_aml_used();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static bool acpi_aml_kern_readable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	ret = !__acpi_aml_access_ok(ACPI_AML_IN_KERN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	      __acpi_aml_readable(&acpi_aml_io.in_crc, ACPI_AML_IN_KERN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return ret;
^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 bool acpi_aml_kern_writable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_KERN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	      __acpi_aml_writable(&acpi_aml_io.out_crc, ACPI_AML_OUT_KERN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static bool acpi_aml_user_readable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ret = !__acpi_aml_access_ok(ACPI_AML_OUT_USER) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	      __acpi_aml_readable(&acpi_aml_io.out_crc, ACPI_AML_OUT_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static bool acpi_aml_user_writable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	ret = !__acpi_aml_access_ok(ACPI_AML_IN_USER) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	      __acpi_aml_writable(&acpi_aml_io.in_crc, ACPI_AML_IN_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int acpi_aml_lock_write(struct circ_buf *circ, unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!__acpi_aml_access_ok(flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (!__acpi_aml_writable(circ, flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	acpi_aml_io.flags |= flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int acpi_aml_lock_read(struct circ_buf *circ, unsigned long flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (!__acpi_aml_access_ok(flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (!__acpi_aml_readable(circ, flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	acpi_aml_io.flags |= flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static void acpi_aml_unlock_fifo(unsigned long flag, bool wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	acpi_aml_io.flags &= ~flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (wakeup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		wake_up_interruptible(&acpi_aml_io.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int acpi_aml_write_kern(const char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	struct circ_buf *crc = &acpi_aml_io.out_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	ret = acpi_aml_lock_write(crc, ACPI_AML_OUT_KERN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/* sync tail before inserting logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	p = &crc->buf[crc->head];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	n = min(len, circ_space_to_end(crc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	memcpy(p, buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/* sync head after inserting logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	acpi_aml_unlock_fifo(ACPI_AML_OUT_KERN, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int acpi_aml_readb_kern(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct circ_buf *crc = &acpi_aml_io.in_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	ret = acpi_aml_lock_read(crc, ACPI_AML_IN_KERN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	/* sync head before removing cmds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	p = &crc->buf[crc->tail];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	ret = (int)*p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	/* sync tail before inserting cmds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	crc->tail = (crc->tail + 1) & (ACPI_AML_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	acpi_aml_unlock_fifo(ACPI_AML_IN_KERN, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * acpi_aml_write_log() - Capture debugger output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * @msg: the debugger output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * This function should be used to implement acpi_os_printf() to filter out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  * the debugger output and store the output into the debugger interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * buffer. Return the size of stored logs or errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static ssize_t acpi_aml_write_log(const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	int count = 0, size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!acpi_aml_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		count = strlen(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		ret = acpi_aml_write_kern(msg + size, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			ret = wait_event_interruptible(acpi_aml_io.wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				acpi_aml_kern_writable());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			 * We need to retry when the condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			 * becomes true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		size += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return size > 0 ? size : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * acpi_aml_read_cmd() - Capture debugger input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * @msg: the debugger input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * @size: the size of the debugger input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  * This function should be used to implement acpi_os_get_line() to capture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * the debugger input commands and store the input commands into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * debugger interface buffer. Return the size of stored commands or errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static ssize_t acpi_aml_read_cmd(char *msg, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 * This is ensured by the running fact of the debugger thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 * unless a bug is introduced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	BUG_ON(!acpi_aml_initialized);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		 * Check each input byte to find the end of the command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		ret = acpi_aml_readb_kern();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			ret = wait_event_interruptible(acpi_aml_io.wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				acpi_aml_kern_readable());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			 * We need to retry when the condition becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			 * true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		*(msg + size) = (char)ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		if (ret == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			 * acpi_os_get_line() requires a zero terminated command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			 * string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			*(msg + size - 1) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return size > 0 ? size : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int acpi_aml_thread(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	acpi_osd_exec_callback function = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	void *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (acpi_aml_io.function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		acpi_aml_io.usages++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		function = acpi_aml_io.function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		context = acpi_aml_io.context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		function(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	acpi_aml_io.usages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (!__acpi_aml_used()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		acpi_aml_io.thread = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		wake_up(&acpi_aml_io.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  * acpi_aml_create_thread() - Create AML debugger thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * @function: the debugger thread callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * @context: the context to be passed to the debugger thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  * This function should be used to implement acpi_os_execute() which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  * used by the ACPICA debugger to create the debugger thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static int acpi_aml_create_thread(acpi_osd_exec_callback function, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct task_struct *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	acpi_aml_io.function = function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	acpi_aml_io.context = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	t = kthread_create(acpi_aml_thread, NULL, "aml");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (IS_ERR(t)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		pr_err("Failed to create AML debugger thread.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		return PTR_ERR(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	acpi_aml_io.thread = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	acpi_set_debugger_thread_id((acpi_thread_id)(unsigned long)t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	wake_up_process(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	return 0;
^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) static int acpi_aml_wait_command_ready(bool single_step,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				       char *buffer, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	if (single_step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_EXECUTE_PROMPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		acpi_os_printf("\n%1c ", ACPI_DEBUGGER_COMMAND_PROMPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	status = acpi_os_get_line(buffer, length, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static int acpi_aml_notify_command_complete(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static int acpi_aml_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 * The debugger interface is being closed, no new user is allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 * during this period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (acpi_aml_io.flags & ACPI_AML_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		goto err_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		 * Only one reader is allowed to initiate the debugger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		 * thread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		if (acpi_aml_active_reader) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			goto err_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			pr_debug("Opening debugger reader.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			acpi_aml_active_reader = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		 * No writer is allowed unless the debugger thread is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		 * ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		if (!(acpi_aml_io.flags & ACPI_AML_OPENED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			goto err_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (acpi_aml_active_reader == file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		pr_debug("Opening debugger interface.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		pr_debug("Initializing debugger thread.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		status = acpi_initialize_debugger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			pr_err("Failed to initialize debugger.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			goto err_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		pr_debug("Debugger thread initialized.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		acpi_aml_io.flags |= ACPI_AML_OPENED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		acpi_aml_io.out_crc.head = acpi_aml_io.out_crc.tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		acpi_aml_io.in_crc.head = acpi_aml_io.in_crc.tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		pr_debug("Debugger interface opened.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	acpi_aml_io.users++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) err_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (acpi_aml_active_reader == file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			acpi_aml_active_reader = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) err_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) static int acpi_aml_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	acpi_aml_io.users--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (file == acpi_aml_active_reader) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		pr_debug("Closing debugger reader.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		acpi_aml_active_reader = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		pr_debug("Closing debugger interface.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		acpi_aml_io.flags |= ACPI_AML_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		 * Wake up all user space/kernel space blocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		 * readers/writers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		wake_up_interruptible(&acpi_aml_io.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		 * Wait all user space/kernel space readers/writers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		 * stop so that ACPICA command loop of the debugger thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		 * should fail all its command line reads after this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		wait_event(acpi_aml_io.wait, !acpi_aml_busy());
^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) 		 * Then we try to terminate the debugger thread if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		 * not terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		pr_debug("Terminating debugger thread.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		acpi_terminate_debugger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		wait_event(acpi_aml_io.wait, !acpi_aml_used());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		pr_debug("Debugger thread terminated.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		mutex_lock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		acpi_aml_io.flags &= ~ACPI_AML_OPENED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	if (acpi_aml_io.users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		pr_debug("Debugger interface closed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		acpi_aml_io.flags &= ~ACPI_AML_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	mutex_unlock(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int acpi_aml_read_user(char __user *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	struct circ_buf *crc = &acpi_aml_io.out_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	ret = acpi_aml_lock_read(crc, ACPI_AML_OUT_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	/* sync head before removing logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	p = &crc->buf[crc->tail];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	n = min(len, circ_count_to_end(crc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if (copy_to_user(buf, p, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	/* sync tail after removing logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, ret >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) static ssize_t acpi_aml_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			     size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (!access_ok(buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		ret = acpi_aml_read_user(buf + size, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 			if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 				ret = wait_event_interruptible(acpi_aml_io.wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 					acpi_aml_user_readable());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 				 * We need to retry when the condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 				 * becomes true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 				if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 					goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 			if (!acpi_aml_running())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 			size += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 			*ppos += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	return size > 0 ? size : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) static int acpi_aml_write_user(const char __user *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	struct circ_buf *crc = &acpi_aml_io.in_crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	ret = acpi_aml_lock_write(crc, ACPI_AML_IN_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	/* sync tail before inserting cmds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	p = &crc->buf[crc->head];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	n = min(len, circ_space_to_end(crc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	if (copy_from_user(p, buf, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	/* sync head after inserting cmds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	acpi_aml_unlock_fifo(ACPI_AML_IN_USER, ret >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static ssize_t acpi_aml_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			      size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (!access_ok(buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 		ret = acpi_aml_write_user(buf + size, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 			if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 				ret = wait_event_interruptible(acpi_aml_io.wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 					acpi_aml_user_writable());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 				 * We need to retry when the condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 				 * becomes true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 				if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 					goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			if (!acpi_aml_running())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			size += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 			count -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 			*ppos += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	return size > 0 ? size : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) static __poll_t acpi_aml_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	__poll_t masks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	poll_wait(file, &acpi_aml_io.wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	if (acpi_aml_user_readable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		masks |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (acpi_aml_user_writable())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		masks |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	return masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static const struct file_operations acpi_aml_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	.read		= acpi_aml_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	.write		= acpi_aml_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	.poll		= acpi_aml_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	.open		= acpi_aml_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	.release	= acpi_aml_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static const struct acpi_debugger_ops acpi_aml_debugger = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	.create_thread		 = acpi_aml_create_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	.read_cmd		 = acpi_aml_read_cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	.write_log		 = acpi_aml_write_log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	.wait_command_ready	 = acpi_aml_wait_command_ready,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	.notify_command_complete = acpi_aml_notify_command_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) static int __init acpi_aml_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	if (acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	/* Initialize AML IO interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	mutex_init(&acpi_aml_io.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	init_waitqueue_head(&acpi_aml_io.wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	acpi_aml_io.out_crc.buf = acpi_aml_io.out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	acpi_aml_io.in_crc.buf = acpi_aml_io.in_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	acpi_aml_dentry = debugfs_create_file("acpidbg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 					      S_IFREG | S_IRUGO | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 					      acpi_debugfs_dir, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 					      &acpi_aml_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	ret = acpi_register_debugger(THIS_MODULE, &acpi_aml_debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		debugfs_remove(acpi_aml_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		acpi_aml_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	acpi_aml_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) static void __exit acpi_aml_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	if (acpi_aml_initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		acpi_unregister_debugger(&acpi_aml_debugger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		debugfs_remove(acpi_aml_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		acpi_aml_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		acpi_aml_initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) module_init(acpi_aml_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) module_exit(acpi_aml_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) MODULE_AUTHOR("Lv Zheng");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) MODULE_DESCRIPTION("ACPI debugger userspace IO driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) MODULE_LICENSE("GPL");