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