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
goldfish_pipe_cmd_locked(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)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
goldfish_pipe_cmd(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)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 */
goldfish_pipe_error_convert(int status)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
goldfish_pin_pages(unsigned long first_page,unsigned long last_page,unsigned int last_page_size,int is_write,struct page * pages[MAX_BUFFERS_PER_COMMAND],unsigned int * iter_last_page_size)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 */
populate_rw_params(struct page ** pages,int pages_count,unsigned long address,unsigned long address_end,unsigned long first_page,unsigned long last_page,unsigned int iter_last_page_size,int is_write,struct goldfish_pipe_command * command)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
transfer_max_buffers(struct goldfish_pipe * pipe,unsigned long address,unsigned long address_end,int is_write,unsigned long last_page,unsigned int last_page_size,s32 * consumed_size,int * status)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
wait_for_host_signal(struct goldfish_pipe * pipe,int is_write)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
goldfish_pipe_read_write(struct file * filp,char __user * buffer,size_t bufflen,int is_write)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
goldfish_pipe_read(struct file * filp,char __user * buffer,size_t bufflen,loff_t * ppos)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
goldfish_pipe_write(struct file * filp,const char __user * buffer,size_t bufflen,loff_t * ppos)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
goldfish_pipe_poll(struct file * filp,poll_table * wait)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
signalled_pipes_add_locked(struct goldfish_pipe_dev * dev,u32 id,u32 flags)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
signalled_pipes_remove_locked(struct goldfish_pipe_dev * dev,struct goldfish_pipe * pipe)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
signalled_pipes_pop_front(struct goldfish_pipe_dev * dev,int * wakes)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
goldfish_interrupt_task(int irq,void * dev_addr)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 */
goldfish_pipe_interrupt(int irq,void * dev_id)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
get_free_pipe_id_locked(struct goldfish_pipe_dev * dev)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 */
to_goldfish_pipe_dev(struct file * 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 */
goldfish_pipe_open(struct inode * inode,struct file * file)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 * Command buffer needs to be allocated on its own page to make sure
712 * it is physically contiguous in host's address space.
713 */
714 BUILD_BUG_ON(sizeof(struct goldfish_pipe_command) > PAGE_SIZE);
715 pipe->command_buffer =
716 (struct goldfish_pipe_command *)__get_free_page(GFP_KERNEL);
717 if (!pipe->command_buffer) {
718 status = -ENOMEM;
719 goto err_pipe;
720 }
721
722 spin_lock_irqsave(&dev->lock, flags);
723
724 id = get_free_pipe_id_locked(dev);
725 if (id < 0) {
726 status = id;
727 goto err_id_locked;
728 }
729
730 dev->pipes[id] = pipe;
731 pipe->id = id;
732 pipe->command_buffer->id = id;
733
734 /* Now tell the emulator we're opening a new pipe. */
735 dev->buffers->open_command_params.rw_params_max_count =
736 MAX_BUFFERS_PER_COMMAND;
737 dev->buffers->open_command_params.command_buffer_ptr =
738 (u64)(unsigned long)__pa(pipe->command_buffer);
739 status = goldfish_pipe_cmd_locked(pipe, PIPE_CMD_OPEN);
740 spin_unlock_irqrestore(&dev->lock, flags);
741 if (status < 0)
742 goto err_cmd;
743 /* All is done, save the pipe into the file's private data field */
744 file->private_data = pipe;
745 return 0;
746
747 err_cmd:
748 spin_lock_irqsave(&dev->lock, flags);
749 dev->pipes[id] = NULL;
750 err_id_locked:
751 spin_unlock_irqrestore(&dev->lock, flags);
752 free_page((unsigned long)pipe->command_buffer);
753 err_pipe:
754 kfree(pipe);
755 return status;
756 }
757
goldfish_pipe_release(struct inode * inode,struct file * filp)758 static int goldfish_pipe_release(struct inode *inode, struct file *filp)
759 {
760 unsigned long flags;
761 struct goldfish_pipe *pipe = filp->private_data;
762 struct goldfish_pipe_dev *dev = pipe->dev;
763
764 /* The guest is closing the channel, so tell the emulator right now */
765 goldfish_pipe_cmd(pipe, PIPE_CMD_CLOSE);
766
767 spin_lock_irqsave(&dev->lock, flags);
768 dev->pipes[pipe->id] = NULL;
769 signalled_pipes_remove_locked(dev, pipe);
770 spin_unlock_irqrestore(&dev->lock, flags);
771
772 filp->private_data = NULL;
773 free_page((unsigned long)pipe->command_buffer);
774 kfree(pipe);
775 return 0;
776 }
777
778 static const struct file_operations goldfish_pipe_fops = {
779 .owner = THIS_MODULE,
780 .read = goldfish_pipe_read,
781 .write = goldfish_pipe_write,
782 .poll = goldfish_pipe_poll,
783 .open = goldfish_pipe_open,
784 .release = goldfish_pipe_release,
785 };
786
init_miscdevice(struct miscdevice * miscdev)787 static void init_miscdevice(struct miscdevice *miscdev)
788 {
789 memset(miscdev, 0, sizeof(*miscdev));
790
791 miscdev->minor = MISC_DYNAMIC_MINOR;
792 miscdev->name = "goldfish_pipe";
793 miscdev->fops = &goldfish_pipe_fops;
794 }
795
write_pa_addr(void * addr,void __iomem * portl,void __iomem * porth)796 static void write_pa_addr(void *addr, void __iomem *portl, void __iomem *porth)
797 {
798 const unsigned long paddr = __pa(addr);
799
800 writel(upper_32_bits(paddr), porth);
801 writel(lower_32_bits(paddr), portl);
802 }
803
goldfish_pipe_device_init(struct platform_device * pdev,struct goldfish_pipe_dev * dev)804 static int goldfish_pipe_device_init(struct platform_device *pdev,
805 struct goldfish_pipe_dev *dev)
806 {
807 int err;
808
809 err = devm_request_threaded_irq(&pdev->dev, dev->irq,
810 goldfish_pipe_interrupt,
811 goldfish_interrupt_task,
812 IRQF_SHARED, "goldfish_pipe", dev);
813 if (err) {
814 dev_err(&pdev->dev, "unable to allocate IRQ for v2\n");
815 return err;
816 }
817
818 init_miscdevice(&dev->miscdev);
819 err = misc_register(&dev->miscdev);
820 if (err) {
821 dev_err(&pdev->dev, "unable to register v2 device\n");
822 return err;
823 }
824
825 dev->pdev_dev = &pdev->dev;
826 dev->first_signalled_pipe = NULL;
827 dev->pipes_capacity = INITIAL_PIPES_CAPACITY;
828 dev->pipes = kzalloc_objs(*dev->pipes, dev->pipes_capacity);
829 if (!dev->pipes) {
830 misc_deregister(&dev->miscdev);
831 return -ENOMEM;
832 }
833
834 /*
835 * We're going to pass two buffers, open_command_params and
836 * signalled_pipe_buffers, to the host. This means each of those buffers
837 * needs to be contained in a single physical page. The easiest choice
838 * is to just allocate a page and place the buffers in it.
839 */
840 BUILD_BUG_ON(sizeof(struct goldfish_pipe_dev_buffers) > PAGE_SIZE);
841 dev->buffers = (struct goldfish_pipe_dev_buffers *)
842 __get_free_page(GFP_KERNEL);
843 if (!dev->buffers) {
844 kfree(dev->pipes);
845 misc_deregister(&dev->miscdev);
846 return -ENOMEM;
847 }
848
849 /* Send the buffer addresses to the host */
850 write_pa_addr(&dev->buffers->signalled_pipe_buffers,
851 dev->base + PIPE_REG_SIGNAL_BUFFER,
852 dev->base + PIPE_REG_SIGNAL_BUFFER_HIGH);
853
854 writel(MAX_SIGNALLED_PIPES,
855 dev->base + PIPE_REG_SIGNAL_BUFFER_COUNT);
856
857 write_pa_addr(&dev->buffers->open_command_params,
858 dev->base + PIPE_REG_OPEN_BUFFER,
859 dev->base + PIPE_REG_OPEN_BUFFER_HIGH);
860
861 platform_set_drvdata(pdev, dev);
862 return 0;
863 }
864
goldfish_pipe_device_deinit(struct platform_device * pdev,struct goldfish_pipe_dev * dev)865 static void goldfish_pipe_device_deinit(struct platform_device *pdev,
866 struct goldfish_pipe_dev *dev)
867 {
868 misc_deregister(&dev->miscdev);
869 kfree(dev->pipes);
870 free_page((unsigned long)dev->buffers);
871 }
872
goldfish_pipe_probe(struct platform_device * pdev)873 static int goldfish_pipe_probe(struct platform_device *pdev)
874 {
875 struct resource *r;
876 struct goldfish_pipe_dev *dev;
877
878 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
879 if (!dev)
880 return -ENOMEM;
881
882 dev->magic = &goldfish_pipe_device_deinit;
883 spin_lock_init(&dev->lock);
884
885 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
886 if (!r || resource_size(r) < PAGE_SIZE) {
887 dev_err(&pdev->dev, "can't allocate i/o page\n");
888 return -EINVAL;
889 }
890 dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
891 if (!dev->base) {
892 dev_err(&pdev->dev, "ioremap failed\n");
893 return -EINVAL;
894 }
895
896 dev->irq = platform_get_irq(pdev, 0);
897 if (dev->irq < 0)
898 return dev->irq;
899
900 /*
901 * Exchange the versions with the host device
902 *
903 * Note: v1 driver used to not report its version, so we write it before
904 * reading device version back: this allows the host implementation to
905 * detect the old driver (if there was no version write before read).
906 */
907 writel(PIPE_DRIVER_VERSION, dev->base + PIPE_REG_VERSION);
908 dev->version = readl(dev->base + PIPE_REG_VERSION);
909 if (WARN_ON(dev->version < PIPE_CURRENT_DEVICE_VERSION))
910 return -EINVAL;
911
912 return goldfish_pipe_device_init(pdev, dev);
913 }
914
goldfish_pipe_remove(struct platform_device * pdev)915 static void goldfish_pipe_remove(struct platform_device *pdev)
916 {
917 struct goldfish_pipe_dev *dev = platform_get_drvdata(pdev);
918
919 goldfish_pipe_device_deinit(pdev, dev);
920 }
921
922 static const struct acpi_device_id goldfish_pipe_acpi_match[] = {
923 { "GFSH0003", 0 },
924 { },
925 };
926 MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match);
927
928 static const struct of_device_id goldfish_pipe_of_match[] = {
929 { .compatible = "google,android-pipe", },
930 {},
931 };
932 MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match);
933
934 static struct platform_driver goldfish_pipe_driver = {
935 .probe = goldfish_pipe_probe,
936 .remove = goldfish_pipe_remove,
937 .driver = {
938 .name = "goldfish_pipe",
939 .of_match_table = goldfish_pipe_of_match,
940 .acpi_match_table = goldfish_pipe_acpi_match,
941 }
942 };
943
944 module_platform_driver(goldfish_pipe_driver);
945 MODULE_AUTHOR("David Turner <digit@google.com>");
946 MODULE_DESCRIPTION("Goldfish virtual device for QEMU pipes");
947 MODULE_LICENSE("GPL v2");
948