1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2012 Intel, Inc. 4 * Copyright (C) 2013 Intel, Inc. 5 * Copyright (C) 2014 Linaro Limited 6 * Copyright (C) 2011-2016 Google, Inc. 7 * 8 * This software is licensed under the terms of the GNU General Public 9 * License version 2, as published by the Free Software Foundation, and 10 * may be copied, distributed, and modified under those terms. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 */ 18 19 /* This source file contains the implementation of a special device driver 20 * that intends to provide a *very* fast communication channel between the 21 * guest system and the QEMU emulator. 22 * 23 * Usage from the guest is simply the following (error handling simplified): 24 * 25 * int fd = open("/dev/qemu_pipe",O_RDWR); 26 * .... write() or read() through the pipe. 27 * 28 * This driver doesn't deal with the exact protocol used during the session. 29 * It is intended to be as simple as something like: 30 * 31 * // do this _just_ after opening the fd to connect to a specific 32 * // emulator service. 33 * const char* msg = "<pipename>"; 34 * if (write(fd, msg, strlen(msg)+1) < 0) { 35 * ... could not connect to <pipename> service 36 * close(fd); 37 * } 38 * 39 * // after this, simply read() and write() to communicate with the 40 * // service. Exact protocol details left as an exercise to the reader. 41 * 42 * This driver is very fast because it doesn't copy any data through 43 * intermediate buffers, since the emulator is capable of translating 44 * guest user addresses into host ones. 45 * 46 * Note that we must however ensure that each user page involved in the 47 * exchange is properly mapped during a transfer. 48 */ 49 50 #include <linux/module.h> 51 #include <linux/interrupt.h> 52 #include <linux/kernel.h> 53 #include <linux/spinlock.h> 54 #include <linux/miscdevice.h> 55 #include <linux/platform_device.h> 56 #include <linux/poll.h> 57 #include <linux/sched.h> 58 #include <linux/bitops.h> 59 #include <linux/slab.h> 60 #include <linux/io.h> 61 #include <linux/dma-mapping.h> 62 #include <linux/mm.h> 63 #include <linux/bug.h> 64 #include "goldfish_pipe_qemu.h" 65 66 /* 67 * Update this when something changes in the driver's behavior so the host 68 * can benefit from knowing it 69 */ 70 enum { 71 PIPE_DRIVER_VERSION = 2, 72 PIPE_CURRENT_DEVICE_VERSION = 2 73 }; 74 75 enum { 76 MAX_BUFFERS_PER_COMMAND = 336, 77 MAX_SIGNALLED_PIPES = 64, 78 INITIAL_PIPES_CAPACITY = 64 79 }; 80 81 struct goldfish_pipe_dev; 82 83 /* A per-pipe command structure, shared with the host */ 84 struct goldfish_pipe_command { 85 s32 cmd; /* PipeCmdCode, guest -> host */ 86 s32 id; /* pipe id, guest -> host */ 87 s32 status; /* command execution status, host -> guest */ 88 s32 reserved; /* to pad to 64-bit boundary */ 89 union { 90 /* Parameters for PIPE_CMD_{READ,WRITE} */ 91 struct { 92 /* number of buffers, guest -> host */ 93 u32 buffers_count; 94 /* number of consumed bytes, host -> guest */ 95 s32 consumed_size; 96 /* buffer pointers, guest -> host */ 97 u64 ptrs[MAX_BUFFERS_PER_COMMAND]; 98 /* buffer sizes, guest -> host */ 99 u32 sizes[MAX_BUFFERS_PER_COMMAND]; 100 } rw_params; 101 }; 102 }; 103 104 /* A single signalled pipe information */ 105 struct signalled_pipe_buffer { 106 u32 id; 107 u32 flags; 108 }; 109 110 /* Parameters for the PIPE_CMD_OPEN command */ 111 struct open_command_param { 112 u64 command_buffer_ptr; 113 u32 rw_params_max_count; 114 }; 115 116 /* Device-level set of buffers shared with the host */ 117 struct goldfish_pipe_dev_buffers { 118 struct open_command_param open_command_params; 119 struct signalled_pipe_buffer 120 signalled_pipe_buffers[MAX_SIGNALLED_PIPES]; 121 }; 122 123 /* This data type models a given pipe instance */ 124 struct goldfish_pipe { 125 /* pipe ID - index into goldfish_pipe_dev::pipes array */ 126 u32 id; 127 128 /* The wake flags pipe is waiting for 129 * Note: not protected with any lock, uses atomic operations 130 * and barriers to make it thread-safe. 131 */ 132 unsigned long flags; 133 134 /* wake flags host have signalled, 135 * - protected by goldfish_pipe_dev::lock 136 */ 137 unsigned long signalled_flags; 138 139 /* A pointer to command buffer */ 140 struct goldfish_pipe_command *command_buffer; 141 142 /* doubly linked list of signalled pipes, protected by 143 * goldfish_pipe_dev::lock 144 */ 145 struct goldfish_pipe *prev_signalled; 146 struct goldfish_pipe *next_signalled; 147 148 /* 149 * A pipe's own lock. Protects the following: 150 * - *command_buffer - makes sure a command can safely write its 151 * parameters to the host and read the results back. 152 */ 153 struct mutex lock; 154 155 /* A wake queue for sleeping until host signals an event */ 156 wait_queue_head_t wake_queue; 157 158 /* Pointer to the parent goldfish_pipe_dev instance */ 159 struct goldfish_pipe_dev *dev; 160 161 /* A buffer of pages, too large to fit into a stack frame */ 162 struct page *pages[MAX_BUFFERS_PER_COMMAND]; 163 }; 164 165 /* The global driver data. Holds a reference to the i/o page used to 166 * communicate with the emulator, and a wake queue for blocked tasks 167 * waiting to be awoken. 168 */ 169 struct goldfish_pipe_dev { 170 /* A magic number to check if this is an instance of this struct */ 171 void *magic; 172 173 /* 174 * Global device spinlock. Protects the following members: 175 * - pipes, pipes_capacity 176 * - [*pipes, *pipes + pipes_capacity) - array data 177 * - first_signalled_pipe, 178 * goldfish_pipe::prev_signalled, 179 * goldfish_pipe::next_signalled, 180 * goldfish_pipe::signalled_flags - all singnalled-related fields, 181 * in all allocated pipes 182 * - open_command_params - PIPE_CMD_OPEN-related buffers 183 * 184 * It looks like a lot of different fields, but the trick is that 185 * the only operation that happens often is the signalled pipes array 186 * manipulation. That's why it's OK for now to keep the rest of the 187 * fields under the same lock. If we notice too much contention because 188 * of PIPE_CMD_OPEN, then we should add a separate lock there. 189 */ 190 spinlock_t lock; 191 192 /* 193 * Array of the pipes of |pipes_capacity| elements, 194 * indexed by goldfish_pipe::id 195 */ 196 struct goldfish_pipe **pipes; 197 u32 pipes_capacity; 198 199 /* Pointers to the buffers host uses for interaction with this driver */ 200 struct goldfish_pipe_dev_buffers *buffers; 201 202 /* Head of a doubly linked list of signalled pipes */ 203 struct goldfish_pipe *first_signalled_pipe; 204 205 /* ptr to platform device's device struct */ 206 struct device *pdev_dev; 207 208 /* Some device-specific data */ 209 int irq; 210 int version; 211 unsigned char __iomem *base; 212 213 struct miscdevice miscdev; 214 }; 215 216 static int goldfish_pipe_cmd_locked(struct goldfish_pipe *pipe, 217 enum PipeCmdCode cmd) 218 { 219 pipe->command_buffer->cmd = cmd; 220 /* failure by default */ 221 pipe->command_buffer->status = PIPE_ERROR_INVAL; 222 writel(pipe->id, pipe->dev->base + PIPE_REG_CMD); 223 return pipe->command_buffer->status; 224 } 225 226 static int goldfish_pipe_cmd(struct goldfish_pipe *pipe, enum PipeCmdCode cmd) 227 { 228 int status; 229 230 if (mutex_lock_interruptible(&pipe->lock)) 231 return PIPE_ERROR_IO; 232 status = goldfish_pipe_cmd_locked(pipe, cmd); 233 mutex_unlock(&pipe->lock); 234 return status; 235 } 236 237 /* 238 * This function converts an error code returned by the emulator through 239 * the PIPE_REG_STATUS i/o register into a valid negative errno value. 240 */ 241 static int goldfish_pipe_error_convert(int status) 242 { 243 switch (status) { 244 case PIPE_ERROR_AGAIN: 245 return -EAGAIN; 246 case PIPE_ERROR_NOMEM: 247 return -ENOMEM; 248 case PIPE_ERROR_IO: 249 return -EIO; 250 default: 251 return -EINVAL; 252 } 253 } 254 255 static int goldfish_pin_pages(unsigned long first_page, 256 unsigned long last_page, 257 unsigned int last_page_size, 258 int is_write, 259 struct page *pages[MAX_BUFFERS_PER_COMMAND], 260 unsigned int *iter_last_page_size) 261 { 262 int ret; 263 int requested_pages = ((last_page - first_page) >> PAGE_SHIFT) + 1; 264 265 if (requested_pages > MAX_BUFFERS_PER_COMMAND) { 266 requested_pages = MAX_BUFFERS_PER_COMMAND; 267 *iter_last_page_size = PAGE_SIZE; 268 } else { 269 *iter_last_page_size = last_page_size; 270 } 271 272 ret = pin_user_pages_fast(first_page, requested_pages, 273 !is_write ? FOLL_WRITE : 0, 274 pages); 275 if (ret <= 0) 276 return -EFAULT; 277 if (ret < requested_pages) 278 *iter_last_page_size = PAGE_SIZE; 279 280 return ret; 281 } 282 283 /* Populate the call parameters, merging adjacent pages together */ 284 static void populate_rw_params(struct page **pages, 285 int pages_count, 286 unsigned long address, 287 unsigned long address_end, 288 unsigned long first_page, 289 unsigned long last_page, 290 unsigned int iter_last_page_size, 291 int is_write, 292 struct goldfish_pipe_command *command) 293 { 294 /* 295 * Process the first page separately - it's the only page that 296 * needs special handling for its start address. 297 */ 298 unsigned long xaddr = page_to_phys(pages[0]); 299 unsigned long xaddr_prev = xaddr; 300 int buffer_idx = 0; 301 int i = 1; 302 int size_on_page = first_page == last_page 303 ? (int)(address_end - address) 304 : (PAGE_SIZE - (address & ~PAGE_MASK)); 305 command->rw_params.ptrs[0] = (u64)(xaddr | (address & ~PAGE_MASK)); 306 command->rw_params.sizes[0] = size_on_page; 307 for (; i < pages_count; ++i) { 308 xaddr = page_to_phys(pages[i]); 309 size_on_page = (i == pages_count - 1) ? 310 iter_last_page_size : PAGE_SIZE; 311 if (xaddr == xaddr_prev + PAGE_SIZE) { 312 command->rw_params.sizes[buffer_idx] += size_on_page; 313 } else { 314 ++buffer_idx; 315 command->rw_params.ptrs[buffer_idx] = (u64)xaddr; 316 command->rw_params.sizes[buffer_idx] = size_on_page; 317 } 318 xaddr_prev = xaddr; 319 } 320 command->rw_params.buffers_count = buffer_idx + 1; 321 } 322 323 static int transfer_max_buffers(struct goldfish_pipe *pipe, 324 unsigned long address, 325 unsigned long address_end, 326 int is_write, 327 unsigned long last_page, 328 unsigned int last_page_size, 329 s32 *consumed_size, 330 int *status) 331 { 332 unsigned long first_page = address & PAGE_MASK; 333 unsigned int iter_last_page_size; 334 int pages_count; 335 336 /* Serialize access to the pipe command buffers */ 337 if (mutex_lock_interruptible(&pipe->lock)) 338 return -ERESTARTSYS; 339 340 pages_count = goldfish_pin_pages(first_page, last_page, 341 last_page_size, is_write, 342 pipe->pages, &iter_last_page_size); 343 if (pages_count < 0) { 344 mutex_unlock(&pipe->lock); 345 return pages_count; 346 } 347 348 populate_rw_params(pipe->pages, pages_count, address, address_end, 349 first_page, last_page, iter_last_page_size, is_write, 350 pipe->command_buffer); 351 352 /* Transfer the data */ 353 *status = goldfish_pipe_cmd_locked(pipe, 354 is_write ? PIPE_CMD_WRITE : PIPE_CMD_READ); 355 356 *consumed_size = pipe->command_buffer->rw_params.consumed_size; 357 358 unpin_user_pages_dirty_lock(pipe->pages, pages_count, 359 !is_write && *consumed_size > 0); 360 361 mutex_unlock(&pipe->lock); 362 return 0; 363 } 364 365 static int wait_for_host_signal(struct goldfish_pipe *pipe, int is_write) 366 { 367 u32 wake_bit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ; 368 369 set_bit(wake_bit, &pipe->flags); 370 371 /* Tell the emulator we're going to wait for a wake event */ 372 goldfish_pipe_cmd(pipe, 373 is_write ? PIPE_CMD_WAKE_ON_WRITE : PIPE_CMD_WAKE_ON_READ); 374 375 while (test_bit(wake_bit, &pipe->flags)) { 376 if (wait_event_interruptible(pipe->wake_queue, 377 !test_bit(wake_bit, &pipe->flags))) 378 return -ERESTARTSYS; 379 380 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)) 381 return -EIO; 382 } 383 384 return 0; 385 } 386 387 static ssize_t goldfish_pipe_read_write(struct file *filp, 388 char __user *buffer, 389 size_t bufflen, 390 int is_write) 391 { 392 struct goldfish_pipe *pipe = filp->private_data; 393 int count = 0, ret = -EINVAL; 394 unsigned long address, address_end, last_page; 395 unsigned int last_page_size; 396 397 /* If the emulator already closed the pipe, no need to go further */ 398 if (unlikely(test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))) 399 return -EIO; 400 /* Null reads or writes succeeds */ 401 if (unlikely(bufflen == 0)) 402 return 0; 403 /* Check the buffer range for access */ 404 if (unlikely(!access_ok(buffer, bufflen))) 405 return -EFAULT; 406 407 address = (unsigned long)buffer; 408 address_end = address + bufflen; 409 last_page = (address_end - 1) & PAGE_MASK; 410 last_page_size = ((address_end - 1) & ~PAGE_MASK) + 1; 411 412 while (address < address_end) { 413 s32 consumed_size; 414 int status; 415 416 ret = transfer_max_buffers(pipe, address, address_end, is_write, 417 last_page, last_page_size, 418 &consumed_size, &status); 419 if (ret < 0) 420 break; 421 422 if (consumed_size > 0) { 423 /* No matter what's the status, we've transferred 424 * something. 425 */ 426 count += consumed_size; 427 address += consumed_size; 428 } 429 if (status > 0) 430 continue; 431 if (status == 0) { 432 /* EOF */ 433 ret = 0; 434 break; 435 } 436 if (count > 0) { 437 /* 438 * An error occurred, but we already transferred 439 * something on one of the previous iterations. 440 * Just return what we already copied and log this 441 * err. 442 */ 443 if (status != PIPE_ERROR_AGAIN) 444 dev_err_ratelimited(pipe->dev->pdev_dev, 445 "backend error %d on %s\n", 446 status, is_write ? "write" : "read"); 447 break; 448 } 449 450 /* 451 * If the error is not PIPE_ERROR_AGAIN, or if we are in 452 * non-blocking mode, just return the error code. 453 */ 454 if (status != PIPE_ERROR_AGAIN || 455 (filp->f_flags & O_NONBLOCK) != 0) { 456 ret = goldfish_pipe_error_convert(status); 457 break; 458 } 459 460 status = wait_for_host_signal(pipe, is_write); 461 if (status < 0) 462 return status; 463 } 464 465 if (count > 0) 466 return count; 467 return ret; 468 } 469 470 static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer, 471 size_t bufflen, loff_t *ppos) 472 { 473 return goldfish_pipe_read_write(filp, buffer, bufflen, 474 /* is_write */ 0); 475 } 476 477 static ssize_t goldfish_pipe_write(struct file *filp, 478 const char __user *buffer, size_t bufflen, 479 loff_t *ppos) 480 { 481 /* cast away the const */ 482 char __user *no_const_buffer = (char __user *)buffer; 483 484 return goldfish_pipe_read_write(filp, no_const_buffer, bufflen, 485 /* is_write */ 1); 486 } 487 488 static __poll_t goldfish_pipe_poll(struct file *filp, poll_table *wait) 489 { 490 struct goldfish_pipe *pipe = filp->private_data; 491 __poll_t mask = 0; 492 int status; 493 494 poll_wait(filp, &pipe->wake_queue, wait); 495 496 status = goldfish_pipe_cmd(pipe, PIPE_CMD_POLL); 497 if (status < 0) 498 return -ERESTARTSYS; 499 500 if (status & PIPE_POLL_IN) 501 mask |= EPOLLIN | EPOLLRDNORM; 502 if (status & PIPE_POLL_OUT) 503 mask |= EPOLLOUT | EPOLLWRNORM; 504 if (status & PIPE_POLL_HUP) 505 mask |= EPOLLHUP; 506 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)) 507 mask |= EPOLLERR; 508 509 return mask; 510 } 511 512 static void signalled_pipes_add_locked(struct goldfish_pipe_dev *dev, 513 u32 id, u32 flags) 514 { 515 struct goldfish_pipe *pipe; 516 517 if (WARN_ON(id >= dev->pipes_capacity)) 518 return; 519 520 pipe = dev->pipes[id]; 521 if (!pipe) 522 return; 523 pipe->signalled_flags |= flags; 524 525 if (pipe->prev_signalled || pipe->next_signalled || 526 dev->first_signalled_pipe == pipe) 527 return; /* already in the list */ 528 pipe->next_signalled = dev->first_signalled_pipe; 529 if (dev->first_signalled_pipe) 530 dev->first_signalled_pipe->prev_signalled = pipe; 531 dev->first_signalled_pipe = pipe; 532 } 533 534 static void signalled_pipes_remove_locked(struct goldfish_pipe_dev *dev, 535 struct goldfish_pipe *pipe) 536 { 537 if (pipe->prev_signalled) 538 pipe->prev_signalled->next_signalled = pipe->next_signalled; 539 if (pipe->next_signalled) 540 pipe->next_signalled->prev_signalled = pipe->prev_signalled; 541 if (pipe == dev->first_signalled_pipe) 542 dev->first_signalled_pipe = pipe->next_signalled; 543 pipe->prev_signalled = NULL; 544 pipe->next_signalled = NULL; 545 } 546 547 static struct goldfish_pipe *signalled_pipes_pop_front( 548 struct goldfish_pipe_dev *dev, int *wakes) 549 { 550 struct goldfish_pipe *pipe; 551 unsigned long flags; 552 553 spin_lock_irqsave(&dev->lock, flags); 554 555 pipe = dev->first_signalled_pipe; 556 if (pipe) { 557 *wakes = pipe->signalled_flags; 558 pipe->signalled_flags = 0; 559 /* 560 * This is an optimized version of 561 * signalled_pipes_remove_locked() 562 * - We want to make it as fast as possible to 563 * wake the sleeping pipe operations faster. 564 */ 565 dev->first_signalled_pipe = pipe->next_signalled; 566 if (dev->first_signalled_pipe) 567 dev->first_signalled_pipe->prev_signalled = NULL; 568 pipe->next_signalled = NULL; 569 } 570 571 spin_unlock_irqrestore(&dev->lock, flags); 572 return pipe; 573 } 574 575 static irqreturn_t goldfish_interrupt_task(int irq, void *dev_addr) 576 { 577 /* Iterate over the signalled pipes and wake them one by one */ 578 struct goldfish_pipe_dev *dev = dev_addr; 579 struct goldfish_pipe *pipe; 580 int wakes; 581 582 while ((pipe = signalled_pipes_pop_front(dev, &wakes)) != NULL) { 583 if (wakes & PIPE_WAKE_CLOSED) { 584 pipe->flags = 1 << BIT_CLOSED_ON_HOST; 585 } else { 586 if (wakes & PIPE_WAKE_READ) 587 clear_bit(BIT_WAKE_ON_READ, &pipe->flags); 588 if (wakes & PIPE_WAKE_WRITE) 589 clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags); 590 } 591 /* 592 * wake_up_interruptible() implies a write barrier, so don't 593 * explicitly add another one here. 594 */ 595 wake_up_interruptible(&pipe->wake_queue); 596 } 597 return IRQ_HANDLED; 598 } 599 600 static void goldfish_pipe_device_deinit(struct platform_device *pdev, 601 struct goldfish_pipe_dev *dev); 602 603 /* 604 * The general idea of the (threaded) interrupt handling: 605 * 606 * 1. device raises an interrupt if there's at least one signalled pipe 607 * 2. IRQ handler reads the signalled pipes and their count from the device 608 * 3. device writes them into a shared buffer and returns the count 609 * it only resets the IRQ if it has returned all signalled pipes, 610 * otherwise it leaves it raised, so IRQ handler will be called 611 * again for the next chunk 612 * 4. IRQ handler adds all returned pipes to the device's signalled pipes list 613 * 5. IRQ handler defers processing the signalled pipes from the list in a 614 * separate context 615 */ 616 static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id) 617 { 618 u32 count; 619 u32 i; 620 unsigned long flags; 621 struct goldfish_pipe_dev *dev = dev_id; 622 623 if (dev->magic != &goldfish_pipe_device_deinit) 624 return IRQ_NONE; 625 626 /* Request the signalled pipes from the device */ 627 spin_lock_irqsave(&dev->lock, flags); 628 629 count = readl(dev->base + PIPE_REG_GET_SIGNALLED); 630 if (count == 0) { 631 spin_unlock_irqrestore(&dev->lock, flags); 632 return IRQ_NONE; 633 } 634 if (count > MAX_SIGNALLED_PIPES) 635 count = MAX_SIGNALLED_PIPES; 636 637 for (i = 0; i < count; ++i) 638 signalled_pipes_add_locked(dev, 639 dev->buffers->signalled_pipe_buffers[i].id, 640 dev->buffers->signalled_pipe_buffers[i].flags); 641 642 spin_unlock_irqrestore(&dev->lock, flags); 643 644 return IRQ_WAKE_THREAD; 645 } 646 647 static int get_free_pipe_id_locked(struct goldfish_pipe_dev *dev) 648 { 649 int id; 650 651 for (id = 0; id < dev->pipes_capacity; ++id) 652 if (!dev->pipes[id]) 653 return id; 654 655 { 656 /* Reallocate the array. 657 * Since get_free_pipe_id_locked runs with interrupts disabled, 658 * we don't want to make calls that could lead to sleep. 659 */ 660 u32 new_capacity = 2 * dev->pipes_capacity; 661 struct goldfish_pipe **pipes = 662 kzalloc_objs(*pipes, new_capacity, GFP_ATOMIC); 663 if (!pipes) 664 return -ENOMEM; 665 memcpy(pipes, dev->pipes, sizeof(*pipes) * dev->pipes_capacity); 666 kfree(dev->pipes); 667 dev->pipes = pipes; 668 id = dev->pipes_capacity; 669 dev->pipes_capacity = new_capacity; 670 } 671 return id; 672 } 673 674 /* A helper function to get the instance of goldfish_pipe_dev from file */ 675 static struct goldfish_pipe_dev *to_goldfish_pipe_dev(struct file *file) 676 { 677 struct miscdevice *miscdev = file->private_data; 678 679 return container_of(miscdev, struct goldfish_pipe_dev, miscdev); 680 } 681 682 /** 683 * goldfish_pipe_open - open a channel to the AVD 684 * @inode: inode of device 685 * @file: file struct of opener 686 * 687 * Create a new pipe link between the emulator and the use application. 688 * Each new request produces a new pipe. 689 * 690 * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit 691 * right now so this is fine. A move to 64bit will need this addressing 692 */ 693 static int goldfish_pipe_open(struct inode *inode, struct file *file) 694 { 695 struct goldfish_pipe_dev *dev = to_goldfish_pipe_dev(file); 696 unsigned long flags; 697 int id; 698 int status; 699 700 /* Allocate new pipe kernel object */ 701 struct goldfish_pipe *pipe = kzalloc_obj(*pipe); 702 703 if (!pipe) 704 return -ENOMEM; 705 706 pipe->dev = dev; 707 mutex_init(&pipe->lock); 708 init_waitqueue_head(&pipe->wake_queue); 709 710 /* 711 * The command buffer is passed to the host as a physical address, so 712 * it must be physically contiguous, which kmalloc() guarantees. 713 */ 714 BUILD_BUG_ON(sizeof(struct goldfish_pipe_command) > PAGE_SIZE); 715 pipe->command_buffer = kmalloc_obj(*pipe->command_buffer); 716 if (!pipe->command_buffer) { 717 status = -ENOMEM; 718 goto err_pipe; 719 } 720 721 spin_lock_irqsave(&dev->lock, flags); 722 723 id = get_free_pipe_id_locked(dev); 724 if (id < 0) { 725 status = id; 726 goto err_id_locked; 727 } 728 729 dev->pipes[id] = pipe; 730 pipe->id = id; 731 pipe->command_buffer->id = id; 732 733 /* Now tell the emulator we're opening a new pipe. */ 734 dev->buffers->open_command_params.rw_params_max_count = 735 MAX_BUFFERS_PER_COMMAND; 736 dev->buffers->open_command_params.command_buffer_ptr = 737 (u64)(unsigned long)__pa(pipe->command_buffer); 738 status = goldfish_pipe_cmd_locked(pipe, PIPE_CMD_OPEN); 739 spin_unlock_irqrestore(&dev->lock, flags); 740 if (status < 0) 741 goto err_cmd; 742 /* All is done, save the pipe into the file's private data field */ 743 file->private_data = pipe; 744 return 0; 745 746 err_cmd: 747 spin_lock_irqsave(&dev->lock, flags); 748 dev->pipes[id] = NULL; 749 err_id_locked: 750 spin_unlock_irqrestore(&dev->lock, flags); 751 kfree(pipe->command_buffer); 752 err_pipe: 753 kfree(pipe); 754 return status; 755 } 756 757 static int goldfish_pipe_release(struct inode *inode, struct file *filp) 758 { 759 unsigned long flags; 760 struct goldfish_pipe *pipe = filp->private_data; 761 struct goldfish_pipe_dev *dev = pipe->dev; 762 763 /* The guest is closing the channel, so tell the emulator right now */ 764 goldfish_pipe_cmd(pipe, PIPE_CMD_CLOSE); 765 766 spin_lock_irqsave(&dev->lock, flags); 767 dev->pipes[pipe->id] = NULL; 768 signalled_pipes_remove_locked(dev, pipe); 769 spin_unlock_irqrestore(&dev->lock, flags); 770 771 filp->private_data = NULL; 772 kfree(pipe->command_buffer); 773 kfree(pipe); 774 return 0; 775 } 776 777 static const struct file_operations goldfish_pipe_fops = { 778 .owner = THIS_MODULE, 779 .read = goldfish_pipe_read, 780 .write = goldfish_pipe_write, 781 .poll = goldfish_pipe_poll, 782 .open = goldfish_pipe_open, 783 .release = goldfish_pipe_release, 784 }; 785 786 static void init_miscdevice(struct miscdevice *miscdev) 787 { 788 memset(miscdev, 0, sizeof(*miscdev)); 789 790 miscdev->minor = MISC_DYNAMIC_MINOR; 791 miscdev->name = "goldfish_pipe"; 792 miscdev->fops = &goldfish_pipe_fops; 793 } 794 795 static void write_pa_addr(void *addr, void __iomem *portl, void __iomem *porth) 796 { 797 const unsigned long paddr = __pa(addr); 798 799 writel(upper_32_bits(paddr), porth); 800 writel(lower_32_bits(paddr), portl); 801 } 802 803 static int goldfish_pipe_device_init(struct platform_device *pdev, 804 struct goldfish_pipe_dev *dev) 805 { 806 int err; 807 808 err = devm_request_threaded_irq(&pdev->dev, dev->irq, 809 goldfish_pipe_interrupt, 810 goldfish_interrupt_task, 811 IRQF_SHARED, "goldfish_pipe", dev); 812 if (err) { 813 dev_err(&pdev->dev, "unable to allocate IRQ for v2\n"); 814 return err; 815 } 816 817 init_miscdevice(&dev->miscdev); 818 err = misc_register(&dev->miscdev); 819 if (err) { 820 dev_err(&pdev->dev, "unable to register v2 device\n"); 821 return err; 822 } 823 824 dev->pdev_dev = &pdev->dev; 825 dev->first_signalled_pipe = NULL; 826 dev->pipes_capacity = INITIAL_PIPES_CAPACITY; 827 dev->pipes = kzalloc_objs(*dev->pipes, dev->pipes_capacity); 828 if (!dev->pipes) { 829 misc_deregister(&dev->miscdev); 830 return -ENOMEM; 831 } 832 833 /* 834 * We're going to pass two buffers, open_command_params and 835 * signalled_pipe_buffers, to the host as physical addresses. This means 836 * each of those buffers needs to be physically contiguous, which 837 * kmalloc() guarantees. 838 */ 839 BUILD_BUG_ON(sizeof(struct goldfish_pipe_dev_buffers) > PAGE_SIZE); 840 dev->buffers = kmalloc_obj(*dev->buffers); 841 if (!dev->buffers) { 842 kfree(dev->pipes); 843 misc_deregister(&dev->miscdev); 844 return -ENOMEM; 845 } 846 847 /* Send the buffer addresses to the host */ 848 write_pa_addr(&dev->buffers->signalled_pipe_buffers, 849 dev->base + PIPE_REG_SIGNAL_BUFFER, 850 dev->base + PIPE_REG_SIGNAL_BUFFER_HIGH); 851 852 writel(MAX_SIGNALLED_PIPES, 853 dev->base + PIPE_REG_SIGNAL_BUFFER_COUNT); 854 855 write_pa_addr(&dev->buffers->open_command_params, 856 dev->base + PIPE_REG_OPEN_BUFFER, 857 dev->base + PIPE_REG_OPEN_BUFFER_HIGH); 858 859 platform_set_drvdata(pdev, dev); 860 return 0; 861 } 862 863 static void goldfish_pipe_device_deinit(struct platform_device *pdev, 864 struct goldfish_pipe_dev *dev) 865 { 866 misc_deregister(&dev->miscdev); 867 kfree(dev->pipes); 868 kfree(dev->buffers); 869 } 870 871 static int goldfish_pipe_probe(struct platform_device *pdev) 872 { 873 struct resource *r; 874 struct goldfish_pipe_dev *dev; 875 876 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 877 if (!dev) 878 return -ENOMEM; 879 880 dev->magic = &goldfish_pipe_device_deinit; 881 spin_lock_init(&dev->lock); 882 883 r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 884 if (!r || resource_size(r) < PAGE_SIZE) { 885 dev_err(&pdev->dev, "can't allocate i/o page\n"); 886 return -EINVAL; 887 } 888 dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE); 889 if (!dev->base) { 890 dev_err(&pdev->dev, "ioremap failed\n"); 891 return -EINVAL; 892 } 893 894 dev->irq = platform_get_irq(pdev, 0); 895 if (dev->irq < 0) 896 return dev->irq; 897 898 /* 899 * Exchange the versions with the host device 900 * 901 * Note: v1 driver used to not report its version, so we write it before 902 * reading device version back: this allows the host implementation to 903 * detect the old driver (if there was no version write before read). 904 */ 905 writel(PIPE_DRIVER_VERSION, dev->base + PIPE_REG_VERSION); 906 dev->version = readl(dev->base + PIPE_REG_VERSION); 907 if (WARN_ON(dev->version < PIPE_CURRENT_DEVICE_VERSION)) 908 return -EINVAL; 909 910 return goldfish_pipe_device_init(pdev, dev); 911 } 912 913 static void goldfish_pipe_remove(struct platform_device *pdev) 914 { 915 struct goldfish_pipe_dev *dev = platform_get_drvdata(pdev); 916 917 goldfish_pipe_device_deinit(pdev, dev); 918 } 919 920 static const struct acpi_device_id goldfish_pipe_acpi_match[] = { 921 { "GFSH0003", 0 }, 922 { }, 923 }; 924 MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match); 925 926 static const struct of_device_id goldfish_pipe_of_match[] = { 927 { .compatible = "google,android-pipe", }, 928 {}, 929 }; 930 MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match); 931 932 static struct platform_driver goldfish_pipe_driver = { 933 .probe = goldfish_pipe_probe, 934 .remove = goldfish_pipe_remove, 935 .driver = { 936 .name = "goldfish_pipe", 937 .of_match_table = goldfish_pipe_of_match, 938 .acpi_match_table = goldfish_pipe_acpi_match, 939 } 940 }; 941 942 module_platform_driver(goldfish_pipe_driver); 943 MODULE_AUTHOR("David Turner <digit@google.com>"); 944 MODULE_DESCRIPTION("Goldfish virtual device for QEMU pipes"); 945 MODULE_LICENSE("GPL v2"); 946