1 /*
2 * Copyright (C) 2012 Intel, Inc.
3 * Copyright (C) 2013 Intel, Inc.
4 * Copyright (C) 2014 Linaro Limited
5 * Copyright (C) 2011-2016 Google, Inc.
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18 /* This source file contains the implementation of a special device driver
19 * that intends to provide a *very* fast communication channel between the
20 * guest system and the QEMU emulator.
21 *
22 * Usage from the guest is simply the following (error handling simplified):
23 *
24 * int fd = open("/dev/qemu_pipe",O_RDWR);
25 * .... write() or read() through the pipe.
26 *
27 * This driver doesn't deal with the exact protocol used during the session.
28 * It is intended to be as simple as something like:
29 *
30 * // do this _just_ after opening the fd to connect to a specific
31 * // emulator service.
32 * const char* msg = "<pipename>";
33 * if (write(fd, msg, strlen(msg)+1) < 0) {
34 * ... could not connect to <pipename> service
35 * close(fd);
36 * }
37 *
38 * // after this, simply read() and write() to communicate with the
39 * // service. Exact protocol details left as an exercise to the reader.
40 *
41 * This driver is very fast because it doesn't copy any data through
42 * intermediate buffers, since the emulator is capable of translating
43 * guest user addresses into host ones.
44 *
45 * Note that we must however ensure that each user page involved in the
46 * exchange is properly mapped during a transfer.
47 */
48
49 #include "goldfish_pipe.h"
50 #include "goldfish_dma.h"
51
52 #define ERR(...) printk(KERN_ERR __VA_ARGS__);
53 #define INFO(...) printk(KERN_INFO __VA_ARGS__);
54 #define DPRINT(...) pr_debug(__VA_ARGS__);
55
56 /*
57 * Update this when something changes in the driver's behavior so the host
58 * can benefit from knowing it
59 * Note: version 2 was an intermediate release and isn't supported anymore.
60 */
61 enum {
62 PIPE_DRIVER_VERSION = 4,
63 PIPE_CURRENT_DEVICE_VERSION = 2
64 };
65
66 /*
67 * IMPORTANT: The following constants must match the ones used and defined
68 * in external/qemu/hw/goldfish_pipe.c in the Android source tree.
69 */
70
71 /* List of bitflags returned in status of CMD_POLL command */
72 enum PipePollFlags {
73 PIPE_POLL_IN = 1 << 0,
74 PIPE_POLL_OUT = 1 << 1,
75 PIPE_POLL_HUP = 1 << 2
76 };
77
78 /* Possible status values used to signal errors - see goldfish_pipe_error_convert */
79 enum PipeErrors {
80 PIPE_ERROR_INVAL = -1,
81 PIPE_ERROR_AGAIN = -2,
82 PIPE_ERROR_NOMEM = -3,
83 PIPE_ERROR_IO = -4
84 };
85
86 /* Bit-flags used to signal events from the emulator */
87 enum PipeWakeFlags {
88 PIPE_WAKE_CLOSED = 1 << 0, /* emulator closed pipe */
89 PIPE_WAKE_READ = 1 << 1, /* pipe can now be read from */
90 PIPE_WAKE_WRITE = 1 << 2, /* pipe can now be written to */
91 PIPE_WAKE_UNLOCK_DMA = 1 << 3, /* pipe's DMA buffer can be safely written to again */
92 };
93
94 /* Bit flags for the 'flags' field */
95 enum PipeFlagsBits {
96 BIT_CLOSED_ON_HOST = 0, /* pipe closed by host */
97 BIT_WAKE_ON_WRITE = 1, /* want to be woken on writes */
98 BIT_WAKE_ON_READ = 2, /* want to be woken on reads */
99 BIT_WAKE_ON_UNLOCK_DMA = 3, /* want to wait for unlock of the DMA buffer */
100 };
101
102 enum PipeRegs {
103 PIPE_REG_CMD = 0,
104
105 PIPE_REG_SIGNAL_BUFFER_HIGH = 4,
106 PIPE_REG_SIGNAL_BUFFER = 8,
107 PIPE_REG_SIGNAL_BUFFER_COUNT = 12,
108
109 PIPE_REG_OPEN_BUFFER_HIGH = 20,
110 PIPE_REG_OPEN_BUFFER = 24,
111
112 PIPE_REG_VERSION = 36,
113
114 PIPE_REG_GET_SIGNALLED = 48,
115 };
116
117 enum PipeCmdCode {
118 PIPE_CMD_OPEN = 1, /* to be used by the pipe device itself */
119 PIPE_CMD_CLOSE,
120 PIPE_CMD_POLL,
121 PIPE_CMD_WRITE,
122 PIPE_CMD_WAKE_ON_WRITE,
123 PIPE_CMD_READ,
124 PIPE_CMD_WAKE_ON_READ,
125
126 /*
127 * TODO(zyy): implement a deferred read/write execution to allow parallel
128 * processing of pipe operations on the host.
129 */
130 PIPE_CMD_WAKE_ON_DONE_IO,
131 PIPE_CMD_DMA_HOST_MAP,
132 PIPE_CMD_DMA_HOST_UNMAP,
133 };
134
135 enum {
136 MAX_BUFFERS_PER_COMMAND = 336,
137 MAX_SIGNALLED_PIPES = 64,
138 INITIAL_PIPES_CAPACITY = 64
139 };
140
141 struct goldfish_pipe_dev;
142 struct goldfish_pipe;
143 struct goldfish_pipe_command;
144
145 /* A per-pipe command structure, shared with the host */
146 struct goldfish_pipe_command {
147 s32 cmd; /* PipeCmdCode, guest -> host */
148 s32 id; /* pipe id, guest -> host */
149 s32 status; /* command execution status, host -> guest */
150 s32 reserved; /* to pad to 64-bit boundary */
151 union {
152 /* Parameters for PIPE_CMD_{READ,WRITE} */
153 struct {
154 u32 buffers_count; /* number of buffers, guest -> host */
155 s32 consumed_size; /* number of consumed bytes, host -> guest */
156 u64 ptrs[MAX_BUFFERS_PER_COMMAND]; /* buffer pointers, guest -> host */
157 u32 sizes[MAX_BUFFERS_PER_COMMAND]; /* buffer sizes, guest -> host */
158 } rw_params;
159 /* Parameters for PIPE_CMD_DMA_HOST_(UN)MAP */
160 struct {
161 u64 dma_paddr;
162 u64 sz;
163 } dma_maphost_params;
164 };
165 };
166
167 /* A single signalled pipe information */
168 struct signalled_pipe_buffer {
169 u32 id;
170 u32 flags;
171 };
172
173 /* Parameters for the PIPE_CMD_OPEN command */
174 struct open_command_param {
175 u64 command_buffer_ptr;
176 u32 rw_params_max_count;
177 };
178
179 /* Device-level set of buffers shared with the host */
180 struct goldfish_pipe_dev_buffers {
181 struct open_command_param open_command_params;
182 struct signalled_pipe_buffer signalled_pipe_buffers[MAX_SIGNALLED_PIPES];
183 };
184
185 /* This data type models a given pipe instance */
186 struct goldfish_pipe {
187 u32 id; /* pipe ID - index into goldfish_pipe_dev::pipes array */
188 unsigned long flags; /* The wake flags pipe is waiting for
189 * Note: not protected with any lock, uses atomic operations
190 * and barriers to make it thread-safe.
191 */
192 unsigned long signalled_flags; /* wake flags host have signalled,
193 * - protected by goldfish_pipe_dev::lock */
194
195 struct goldfish_pipe_command *command_buffer; /* A pointer to command buffer */
196
197 /* doubly linked list of signalled pipes, protected by goldfish_pipe_dev::lock */
198 struct goldfish_pipe *prev_signalled;
199 struct goldfish_pipe *next_signalled;
200
201 /*
202 * A pipe's own lock. Protects the following:
203 * - *command_buffer - makes sure a command can safely write its parameters
204 * to the host and read the results back.
205 */
206 struct mutex lock;
207
208 wait_queue_head_t wake_queue; /* A wake queue for sleeping until host signals an event */
209 struct goldfish_pipe_dev *dev; /* Pointer to the parent goldfish_pipe_dev instance */
210 struct goldfish_dma_context *dma; /* Holds information about reserved DMA region for this pipe */
211 /* Says whether or not this pipe is in the middle of a release operation. This can affect
212 * concurrent device operations. However, note that there are two types of concurrency:
213 * 1. Guest does pipe operations from two separate threads/processes for the same pipe fd.
214 * 2. Host performs a pipe operation at the same time as the guest.
215 * We do not use the pipe for (1) unless the pipe is used as a DMA region, in which case it
216 * will be shared across processes and we can have potential simultaneous close/ioctl/mmap
217 * operations. For (2), we need to make sure that all pipe cleanup operations take place after
218 * shutting down all host -> guest communications. |closing| protects both kinds of access.
219 * TODO: Test concurrent pipe read/write as well */
220 bool closing;
221 };
222
223 struct goldfish_pipe_dev goldfish_pipe_dev[1] = {};
224 EXPORT_SYMBOL(goldfish_pipe_dev);
225
goldfish_pipe_cmd_locked(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)226 static int goldfish_pipe_cmd_locked(struct goldfish_pipe *pipe, enum PipeCmdCode cmd)
227 {
228 pipe->command_buffer->cmd = cmd;
229 pipe->command_buffer->status = PIPE_ERROR_INVAL; /* failure by default */
230 writel(pipe->id, pipe->dev->base + PIPE_REG_CMD);
231 return pipe->command_buffer->status;
232 }
233
goldfish_pipe_cmd(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)234 static int goldfish_pipe_cmd(struct goldfish_pipe *pipe, enum PipeCmdCode cmd)
235 {
236 int status;
237 if (mutex_lock_interruptible(&pipe->lock))
238 return PIPE_ERROR_IO;
239 status = goldfish_pipe_cmd_locked(pipe, cmd);
240 mutex_unlock(&pipe->lock);
241 return status;
242 }
243
244 /*
245 * This function converts an error code returned by the emulator through
246 * the PIPE_REG_STATUS i/o register into a valid negative errno value.
247 */
goldfish_pipe_error_convert(int status)248 static int goldfish_pipe_error_convert(int status)
249 {
250 switch (status) {
251 case PIPE_ERROR_AGAIN:
252 return -EAGAIN;
253 case PIPE_ERROR_NOMEM:
254 return -ENOMEM;
255 case PIPE_ERROR_IO:
256 return -EIO;
257 default:
258 return -EINVAL;
259 }
260 }
261
pin_user_pages(unsigned long first_page,unsigned long last_page,unsigned last_page_size,int is_write,struct page * pages[MAX_BUFFERS_PER_COMMAND],unsigned * iter_last_page_size)262 static int pin_user_pages(unsigned long first_page, unsigned long last_page,
263 unsigned last_page_size, int is_write,
264 struct page *pages[MAX_BUFFERS_PER_COMMAND], unsigned *iter_last_page_size)
265 {
266 int ret;
267 int requested_pages = ((last_page - first_page) >> PAGE_SHIFT) + 1;
268 if (requested_pages > MAX_BUFFERS_PER_COMMAND) {
269 requested_pages = MAX_BUFFERS_PER_COMMAND;
270 *iter_last_page_size = PAGE_SIZE;
271 } else {
272 *iter_last_page_size = last_page_size;
273 }
274
275 ret = get_user_pages_fast(
276 first_page, requested_pages, !is_write, pages);
277 if (ret <= 0)
278 return -EFAULT;
279 if (ret < requested_pages)
280 *iter_last_page_size = PAGE_SIZE;
281 return ret;
282
283 }
284
release_user_pages(struct page ** pages,int pages_count,int is_write,s32 consumed_size)285 static void release_user_pages(struct page **pages, int pages_count,
286 int is_write, s32 consumed_size)
287 {
288 int i;
289 for (i = 0; i < pages_count; i++) {
290 if (!is_write && consumed_size > 0) {
291 set_page_dirty(pages[i]);
292 }
293 put_page(pages[i]);
294 }
295 }
296
297 /* 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 iter_last_page_size,int is_write,struct goldfish_pipe_command * command)298 static void populate_rw_params(
299 struct page **pages, int pages_count,
300 unsigned long address, unsigned long address_end,
301 unsigned long first_page, unsigned long last_page,
302 unsigned iter_last_page_size, int is_write,
303 struct goldfish_pipe_command *command)
304 {
305 /*
306 * Process the first page separately - it's the only page that
307 * needs special handling for its start address.
308 */
309 unsigned long xaddr = page_to_phys(pages[0]);
310 unsigned long xaddr_prev = xaddr;
311 int buffer_idx = 0;
312 int i = 1;
313 int size_on_page = first_page == last_page
314 ? (int)(address_end - address)
315 : (PAGE_SIZE - (address & ~PAGE_MASK));
316 command->rw_params.ptrs[0] = (u64)(xaddr | (address & ~PAGE_MASK));
317 command->rw_params.sizes[0] = size_on_page;
318 for (; i < pages_count; ++i) {
319 xaddr = page_to_phys(pages[i]);
320 size_on_page = (i == pages_count - 1) ? iter_last_page_size : PAGE_SIZE;
321 if (xaddr == xaddr_prev + PAGE_SIZE) {
322 command->rw_params.sizes[buffer_idx] += size_on_page;
323 } else {
324 ++buffer_idx;
325 command->rw_params.ptrs[buffer_idx] = (u64)xaddr;
326 command->rw_params.sizes[buffer_idx] = size_on_page;
327 }
328 xaddr_prev = xaddr;
329 }
330 command->rw_params.buffers_count = buffer_idx + 1;
331 }
332
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)333 static int transfer_max_buffers(struct goldfish_pipe *pipe,
334 unsigned long address, unsigned long address_end, int is_write,
335 unsigned long last_page, unsigned int last_page_size,
336 s32* consumed_size, int *status)
337 {
338 struct page *pages[MAX_BUFFERS_PER_COMMAND];
339 unsigned long first_page = address & PAGE_MASK;
340 unsigned int iter_last_page_size;
341 int pages_count = pin_user_pages(first_page, last_page,
342 last_page_size, is_write,
343 pages, &iter_last_page_size);
344 if (pages_count < 0)
345 return pages_count;
346
347 /* Serialize access to the pipe command buffers */
348 if (mutex_lock_interruptible(&pipe->lock))
349 return -ERESTARTSYS;
350
351 populate_rw_params(pages, pages_count, address, address_end,
352 first_page, last_page, iter_last_page_size, is_write,
353 pipe->command_buffer);
354
355 /* Transfer the data */
356 *status = goldfish_pipe_cmd_locked(pipe,
357 is_write ? PIPE_CMD_WRITE : PIPE_CMD_READ);
358
359 *consumed_size = pipe->command_buffer->rw_params.consumed_size;
360
361 mutex_unlock(&pipe->lock);
362
363 release_user_pages(pages, pages_count, is_write, *consumed_size);
364
365 return 0;
366 }
367
goldfish_pipe_wait_event(u32 wakeBit,struct goldfish_pipe * pipe)368 static int goldfish_pipe_wait_event(u32 wakeBit, struct goldfish_pipe *pipe) {
369 while (test_bit(wakeBit, &pipe->flags)) {
370 if (wait_event_interruptible(
371 pipe->wake_queue,
372 !test_bit(wakeBit, &pipe->flags)))
373 return -ERESTARTSYS;
374
375 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
376 return -EIO;
377 }
378 return 0;
379 }
380
wait_for_host_signal(struct goldfish_pipe * pipe,int is_write)381 static int wait_for_host_signal(struct goldfish_pipe *pipe, int is_write)
382 {
383 u32 wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
384 set_bit(wakeBit, &pipe->flags);
385
386 /* Tell the emulator we're going to wait for a wake event */
387 (void)goldfish_pipe_cmd(pipe,
388 is_write ? PIPE_CMD_WAKE_ON_WRITE : PIPE_CMD_WAKE_ON_READ);
389
390 return goldfish_pipe_wait_event(wakeBit, pipe);
391 }
392
goldfish_pipe_read_write(struct file * filp,char __user * buffer,size_t bufflen,int is_write)393 static ssize_t goldfish_pipe_read_write(struct file *filp,
394 char __user *buffer, size_t bufflen, int is_write)
395 {
396 struct goldfish_pipe *pipe = filp->private_data;
397 int count = 0, ret = -EINVAL;
398 unsigned long address, address_end, last_page;
399 unsigned int last_page_size;
400
401 /* If the emulator already closed the pipe, no need to go further */
402 if (unlikely(test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)))
403 return -EIO;
404 /* Null reads or writes succeeds */
405 if (unlikely(bufflen == 0))
406 return 0;
407 /* Check the buffer range for access */
408 if (unlikely(!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ,
409 buffer, bufflen)))
410 return -EFAULT;
411
412 address = (unsigned long)buffer;
413 address_end = address + bufflen;
414 last_page = (address_end - 1) & PAGE_MASK;
415 last_page_size = ((address_end - 1) & ~PAGE_MASK) + 1;
416
417 while (address < address_end) {
418 s32 consumed_size;
419 int status;
420 ret = transfer_max_buffers(pipe, address, address_end, is_write,
421 last_page, last_page_size, &consumed_size, &status);
422 if (ret < 0)
423 break;
424
425 if (consumed_size > 0) {
426 /* No matter what's the status, we've transfered something */
427 count += consumed_size;
428 address += consumed_size;
429 }
430 if (status > 0)
431 continue;
432 if (status == 0) {
433 /* EOF */
434 ret = 0;
435 break;
436 }
437 if (count > 0) {
438 /*
439 * An error occured, but we already transfered
440 * something on one of the previous iterations.
441 * Just return what we already copied and log this
442 * err.
443 */
444 if (status != PIPE_ERROR_AGAIN)
445 pr_info_ratelimited("goldfish_pipe: 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 || (filp->f_flags & O_NONBLOCK) != 0) {
455 ret = goldfish_pipe_error_convert(status);
456 break;
457 }
458
459 status = wait_for_host_signal(pipe, is_write);
460 if (status < 0)
461 return status;
462 }
463
464 if (count > 0)
465 return count;
466 return ret;
467 }
468
goldfish_pipe_read(struct file * filp,char __user * buffer,size_t bufflen,loff_t * ppos)469 static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
470 size_t bufflen, loff_t *ppos)
471 {
472 return goldfish_pipe_read_write(filp, buffer, bufflen, /* is_write */ 0);
473 }
474
goldfish_pipe_write(struct file * filp,const char __user * buffer,size_t bufflen,loff_t * ppos)475 static ssize_t goldfish_pipe_write(struct file *filp,
476 const char __user *buffer, size_t bufflen,
477 loff_t *ppos)
478 {
479 return goldfish_pipe_read_write(filp,
480 /* cast away the const */(char __user *)buffer, bufflen,
481 /* is_write */ 1);
482 }
483
goldfish_pipe_poll(struct file * filp,poll_table * wait)484 static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
485 {
486 struct goldfish_pipe *pipe = filp->private_data;
487 unsigned int mask = 0;
488 int status;
489
490 poll_wait(filp, &pipe->wake_queue, wait);
491
492 status = goldfish_pipe_cmd(pipe, PIPE_CMD_POLL);
493 if (status < 0) {
494 return -ERESTARTSYS;
495 }
496
497 if (status & PIPE_POLL_IN)
498 mask |= POLLIN | POLLRDNORM;
499 if (status & PIPE_POLL_OUT)
500 mask |= POLLOUT | POLLWRNORM;
501 if (status & PIPE_POLL_HUP)
502 mask |= POLLHUP;
503 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
504 mask |= POLLERR;
505
506 return mask;
507 }
508
signalled_pipes_add_locked(struct goldfish_pipe_dev * dev,u32 id,u32 flags)509 static void signalled_pipes_add_locked(struct goldfish_pipe_dev *dev,
510 u32 id, u32 flags)
511 {
512 struct goldfish_pipe *pipe;
513
514 BUG_ON(id >= dev->pipes_capacity);
515
516 pipe = dev->pipes[id];
517 if (!pipe)
518 return;
519 pipe->signalled_flags |= flags;
520
521 if (pipe->prev_signalled || pipe->next_signalled
522 || dev->first_signalled_pipe == pipe)
523 return; /* already in the list */
524 pipe->next_signalled = dev->first_signalled_pipe;
525 if (dev->first_signalled_pipe) {
526 dev->first_signalled_pipe->prev_signalled = pipe;
527 }
528 dev->first_signalled_pipe = pipe;
529 }
530
signalled_pipes_remove_locked(struct goldfish_pipe_dev * dev,struct goldfish_pipe * pipe)531 static void signalled_pipes_remove_locked(struct goldfish_pipe_dev *dev,
532 struct goldfish_pipe *pipe) {
533 if (pipe->prev_signalled)
534 pipe->prev_signalled->next_signalled = pipe->next_signalled;
535 if (pipe->next_signalled)
536 pipe->next_signalled->prev_signalled = pipe->prev_signalled;
537 if (pipe == dev->first_signalled_pipe)
538 dev->first_signalled_pipe = pipe->next_signalled;
539 pipe->prev_signalled = NULL;
540 pipe->next_signalled = NULL;
541 }
542
signalled_pipes_pop_front(struct goldfish_pipe_dev * dev,int * wakes)543 static struct goldfish_pipe *signalled_pipes_pop_front(struct goldfish_pipe_dev *dev,
544 int *wakes)
545 {
546 struct goldfish_pipe *pipe;
547 unsigned long flags;
548 spin_lock_irqsave(&dev->lock, flags);
549
550 pipe = dev->first_signalled_pipe;
551 if (pipe) {
552 *wakes = pipe->signalled_flags;
553 pipe->signalled_flags = 0;
554 /*
555 * This is an optimized version of signalled_pipes_remove_locked() -
556 * we want to make it as fast as possible to wake the sleeping pipe
557 * operations faster
558 */
559 dev->first_signalled_pipe = pipe->next_signalled;
560 if (dev->first_signalled_pipe)
561 dev->first_signalled_pipe->prev_signalled = NULL;
562 pipe->next_signalled = NULL;
563 }
564
565 spin_unlock_irqrestore(&dev->lock, flags);
566 return pipe;
567 }
568
goldfish_pipe_dma_clear_lock(struct goldfish_pipe * pipe)569 static void goldfish_pipe_dma_clear_lock(struct goldfish_pipe *pipe) {
570 DPRINT("PIPE_WAKE_UNLOCK_DMA: unlock pipe dma for pipe 0x%p\n", pipe);
571 if (!pipe->closing && pipe->dma) {
572 WARN_ON(!pipe->dma->locked);
573 clear_bit(BIT_WAKE_ON_UNLOCK_DMA, &pipe->flags);
574 pipe->dma->locked = false;
575 /* meant to be used with wake_up_interruptible---otherwise no signaling,
576 * and no write barrier! */
577 }
578 }
579
goldfish_interrupt_task(unsigned long unused)580 static void goldfish_interrupt_task(unsigned long unused)
581 {
582 /* Iterate over the signalled pipes and wake them one by one */
583 struct goldfish_pipe *pipe;
584 int wakes;
585 while ((pipe = signalled_pipes_pop_front(goldfish_pipe_dev, &wakes)) !=
586 NULL) {
587 if (pipe->closing) return;
588 if (wakes & PIPE_WAKE_CLOSED) {
589 pipe->flags = 1 << BIT_CLOSED_ON_HOST;
590 } else {
591 if (wakes & PIPE_WAKE_UNLOCK_DMA)
592 goldfish_pipe_dma_clear_lock(pipe);
593 if (wakes & PIPE_WAKE_READ)
594 clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
595 if (wakes & PIPE_WAKE_WRITE)
596 clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
597 }
598 /*
599 * wake_up_interruptible() implies a write barrier, so don't explicitly
600 * add another one here.
601 */
602 wake_up_interruptible(&pipe->wake_queue);
603 }
604 }
605 DECLARE_TASKLET(goldfish_interrupt_tasklet, goldfish_interrupt_task, 0);
606
607 /*
608 * The general idea of the interrupt handling:
609 *
610 * 1. device raises an interrupt if there's at least one signalled pipe
611 * 2. IRQ handler reads the signalled pipes and their count from the device
612 * 3. device writes them into a shared buffer and returns the count
613 * it only resets the IRQ if it has returned all signalled pipes,
614 * otherwise it leaves it raised, so IRQ handler will be called
615 * again for the next chunk
616 * 4. IRQ handler adds all returned pipes to the device's signalled pipes list
617 * 5. IRQ handler launches a tasklet to process the signalled pipes from the
618 * list in a separate context
619 */
goldfish_pipe_interrupt(int irq,void * dev_id)620 static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
621 {
622 u32 count;
623 u32 i;
624 unsigned long flags;
625 struct goldfish_pipe_dev *dev = dev_id;
626 if (dev != goldfish_pipe_dev)
627 return IRQ_NONE;
628
629 /* Request the signalled pipes from the device */
630 spin_lock_irqsave(&dev->lock, flags);
631
632 count = readl(dev->base + PIPE_REG_GET_SIGNALLED);
633 if (count == 0) {
634 spin_unlock_irqrestore(&dev->lock, flags);
635 return IRQ_NONE;
636 }
637 if (count > MAX_SIGNALLED_PIPES)
638 count = MAX_SIGNALLED_PIPES;
639
640 for (i = 0; i < count; ++i)
641 signalled_pipes_add_locked(dev,
642 dev->buffers->signalled_pipe_buffers[i].id,
643 dev->buffers->signalled_pipe_buffers[i].flags);
644
645 spin_unlock_irqrestore(&dev->lock, flags);
646
647 tasklet_schedule(&goldfish_interrupt_tasklet);
648 return IRQ_HANDLED;
649 }
650
get_free_pipe_id_locked(struct goldfish_pipe_dev * dev)651 static int get_free_pipe_id_locked(struct goldfish_pipe_dev *dev)
652 {
653 int id;
654 for (id = 0; id < dev->pipes_capacity; ++id)
655 if (!dev->pipes[id])
656 return id;
657
658 {
659 /* Reallocate the array */
660 u32 new_capacity = 2 * dev->pipes_capacity;
661 struct goldfish_pipe **pipes =
662 kcalloc(new_capacity, sizeof(*pipes), GFP_KERNEL);
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 /**
675 * goldfish_pipe_open - open a channel to the AVD
676 * @inode: inode of device
677 * @file: file struct of opener
678 *
679 * Create a new pipe link between the emulator and the use application.
680 * Each new request produces a new pipe.
681 *
682 * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit
683 * right now so this is fine. A move to 64bit will need this addressing
684 */
goldfish_pipe_open(struct inode * inode,struct file * file)685 static int goldfish_pipe_open(struct inode *inode, struct file *file)
686 {
687 struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
688 unsigned long flags;
689 int id;
690 int status;
691
692 /* Allocate new pipe kernel object */
693 struct goldfish_pipe *pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
694 if (pipe == NULL) {
695 ERR("Could not allocate new pipe!\n");
696 return -ENOMEM;
697 }
698
699 pipe->dev = dev;
700 mutex_init(&pipe->lock);
701 init_waitqueue_head(&pipe->wake_queue);
702
703 /*
704 * Command buffer needs to be allocated on its own page to make sure it is
705 * physically contiguous in host's address space.
706 */
707 pipe->command_buffer =
708 (struct goldfish_pipe_command *)__get_free_page(GFP_KERNEL);
709 if (!pipe->command_buffer) {
710 ERR("Could not alloc pipe command buffer!\n");
711 status = -ENOMEM;
712 goto err_pipe;
713 }
714
715 spin_lock_irqsave(&dev->lock, flags);
716
717 id = get_free_pipe_id_locked(dev);
718 if (id < 0) {
719 ERR("Could not get free pipe id!\n");
720 status = id;
721 goto err_id_locked;
722 }
723
724 dev->pipes[id] = pipe;
725 pipe->id = id;
726 pipe->command_buffer->id = id;
727
728 /* Now tell the emulator we're opening a new pipe. */
729 dev->buffers->open_command_params.rw_params_max_count =
730 MAX_BUFFERS_PER_COMMAND;
731 dev->buffers->open_command_params.command_buffer_ptr =
732 (u64)(unsigned long)__pa(pipe->command_buffer);
733 status = goldfish_pipe_cmd_locked(pipe, PIPE_CMD_OPEN);
734 spin_unlock_irqrestore(&dev->lock, flags);
735 if (status < 0) {
736 ERR("Could not tell host of new pipe! status=%d", status);
737 goto err_cmd;
738 }
739
740 pipe->dma = NULL;
741
742 /* All is done, save the pipe into the file's private data field */
743 file->private_data = pipe;
744 DPRINT("%s on 0x%p\n", __FUNCTION__, 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_dma_release_host_locked(struct goldfish_pipe * pipe)758 static void goldfish_pipe_dma_release_host_locked(struct goldfish_pipe *pipe) {
759 struct goldfish_dma_context *dma = pipe->dma;
760 if (!dma) return;
761
762 if (dma->dma_vaddr) {
763 DPRINT("Last ref for dma region @ 0x%llx\n", dma->phys_begin);
764 pipe->command_buffer->dma_maphost_params.dma_paddr = dma->phys_begin;
765 pipe->command_buffer->dma_maphost_params.sz = dma->dma_size;
766 goldfish_pipe_cmd(pipe, PIPE_CMD_DMA_HOST_UNMAP);
767 }
768 DPRINT("after delete of dma @ 0x%llx: alloc total %llu\n",
769 dma->phys_begin, pipe->dev->dma_alloc_total);
770 dma->locked = false;
771 }
772
goldfish_pipe_dma_release_guest_locked(struct goldfish_pipe * pipe)773 static void goldfish_pipe_dma_release_guest_locked(struct goldfish_pipe *pipe) {
774 struct goldfish_dma_context *dma = pipe->dma;
775 if (!dma) return;
776
777 if (dma->dma_vaddr) {
778 dma_free_coherent(
779 dma->pdev_dev,
780 dma->dma_size,
781 dma->dma_vaddr,
782 dma->phys_begin);
783 pipe->dev->dma_alloc_total -= dma->dma_size;
784 DPRINT("after delete of dma @ 0x%llx: alloc total %llu\n",
785 dma->phys_begin, pipe->dev->dma_alloc_total);
786 }
787 }
788
goldfish_pipe_release(struct inode * inode,struct file * filp)789 static int goldfish_pipe_release(struct inode *inode, struct file *filp)
790 {
791 unsigned long flags;
792 struct goldfish_pipe *pipe = filp->private_data;
793 struct goldfish_pipe_dev *dev = pipe->dev;
794
795 DPRINT("%s on 0x%p\n", __FUNCTION__, pipe);
796
797 pipe->closing = true;
798
799 if (pipe->dma) mutex_lock(&pipe->dma->mutex_lock);
800
801 /* The guest is closing the channel, so tell the emulator right now */
802 goldfish_pipe_dma_release_host_locked(pipe);
803 (void)goldfish_pipe_cmd(pipe, PIPE_CMD_CLOSE);
804
805 spin_lock_irqsave(&dev->lock, flags);
806 dev->pipes[pipe->id] = NULL;
807 signalled_pipes_remove_locked(dev, pipe);
808 spin_unlock_irqrestore(&dev->lock, flags);
809
810 filp->private_data = NULL;
811
812 /* Even if a fd is duped or involved in a forked process,
813 * open/release methods are called only once, ever.
814 * This makes goldfish_pipe_release a safe point
815 * to delete the DMA region. */
816 goldfish_pipe_dma_release_guest_locked(pipe);
817
818 if (pipe->dma) {
819 mutex_unlock(&pipe->dma->mutex_lock);
820 kfree(pipe->dma);
821 pipe->dma = NULL;
822 }
823
824 free_page((unsigned long)pipe->command_buffer);
825 kfree(pipe);
826
827 return 0;
828 }
829
830 /* VMA open/close are for debugging purposes only.
831 * One might think that fork() (and thus pure calls to open())
832 * will require some sort of bookkeeping or refcounting
833 * for dma contexts (incl. when to call dma_free_coherent),
834 * but |vm_private_data| field and |vma_open/close| are only
835 * for situations where the driver needs to interact with vma's
836 * directly with its own per-VMA data structure (which does
837 * need to be refcounted).
838 *
839 * Here, we just use the kernel's existing
840 * VMA processing; we don't do anything on our own.
841 * The only reason we would want to do so is if we had to do
842 * special processing for the virtual (not physical) memory
843 * already associated with DMA memory; it is much less related
844 * to the task of knowing when to alloc/dealloc DMA memory. */
goldfish_dma_vma_open(struct vm_area_struct * vma)845 static void goldfish_dma_vma_open(struct vm_area_struct *vma) {
846 /* Not used */
847 }
848
goldfish_dma_vma_close(struct vm_area_struct * vma)849 static void goldfish_dma_vma_close(struct vm_area_struct *vma) {
850 /* Not used */
851 }
852
853 static struct vm_operations_struct goldfish_dma_vm_ops = {
854 .open = goldfish_dma_vma_open,
855 .close = goldfish_dma_vma_close,
856 };
857
is_page_size_multiple(unsigned long sz)858 static bool is_page_size_multiple(unsigned long sz) {
859 return !(sz & (PAGE_SIZE - 1));
860 }
861
goldfish_pipe_dma_alloc_locked(struct goldfish_pipe * pipe)862 static void goldfish_pipe_dma_alloc_locked(struct goldfish_pipe *pipe) {
863 struct goldfish_dma_context *dma;
864
865 DPRINT("%s: try alloc dma for pipe 0x%p\n",
866 __FUNCTION__, pipe);
867
868 dma = pipe->dma;
869
870 if (dma->dma_vaddr) {
871 DPRINT("%s: already alloced, return.\n",
872 __FUNCTION__);
873 return;
874 }
875
876 dma->phys_begin = 0;
877 dma->dma_vaddr =
878 dma_alloc_coherent(
879 dma->pdev_dev,
880 dma->dma_size,
881 (dma_addr_t *)&dma->phys_begin,
882 GFP_KERNEL);
883 BUG_ON(!dma->dma_vaddr);
884
885 dma->phys_end = dma->phys_begin + dma->dma_size;
886 dma->pfn = dma->phys_begin >> PAGE_SHIFT;
887 pipe->dev->dma_alloc_total += dma->dma_size;
888
889 DPRINT("%s: got v/p addrs "
890 "0x%p 0x%llx sz %llu total alloc %llu\n",
891 __FUNCTION__,
892 dma->dma_vaddr,
893 dma->phys_begin,
894 dma->dma_size,
895 pipe->dev->dma_alloc_total);
896 pipe->command_buffer->dma_maphost_params.dma_paddr = dma->phys_begin;
897 pipe->command_buffer->dma_maphost_params.sz = dma->dma_size;
898 goldfish_pipe_cmd(pipe, PIPE_CMD_DMA_HOST_MAP);
899 }
900
901 /* When we call mmap() on a pipe fd, we obtain a pointer into
902 * the physically contiguous DMA region of the pipe device
903 * (Goldfish DMA). */
goldfish_dma_mmap(struct file * filp,struct vm_area_struct * vma)904 static int goldfish_dma_mmap(struct file *filp, struct vm_area_struct *vma) {
905
906 struct goldfish_pipe *pipe = (struct goldfish_pipe *)(filp->private_data);
907 struct goldfish_dma_context *dma = pipe->dma;
908 unsigned long sz_requested = vma->vm_end - vma->vm_start;
909 int map_err;
910
911 if (pipe->closing) return -EINVAL;
912
913 if (!is_page_size_multiple(sz_requested)) {
914 ERR("Cannot mmap dma buffer of size %lx (is not multiple of page size)\n",
915 sz_requested);
916 return -EINVAL;
917 }
918
919 DPRINT("Mapping dma at 0x%llx\n", dma->phys_begin);
920
921 mutex_lock(&dma->mutex_lock);
922 /* Alloc phys region if not allocated already. */
923 goldfish_pipe_dma_alloc_locked(pipe);
924 mutex_unlock(&dma->mutex_lock);
925
926 map_err =
927 remap_pfn_range(
928 vma,
929 vma->vm_start,
930 dma->phys_begin >> PAGE_SHIFT,
931 sz_requested,
932 vma->vm_page_prot);
933
934 if (map_err < 0) {
935 ERR("Cannot remap pfn range....\n");
936 return -EAGAIN;
937 }
938
939 vma->vm_ops = &goldfish_dma_vm_ops;
940 DPRINT("goldfish_dma_mmap for host vaddr 0x%llx succeeded\n",
941 dma->phys_begin);
942
943 return 0;
944 }
945
goldfish_pipe_dma_create_region(struct goldfish_pipe * pipe,uint64_t size)946 static void goldfish_pipe_dma_create_region(
947 struct goldfish_pipe *pipe,
948 uint64_t size) {
949
950 struct goldfish_dma_context *dma =
951 kzalloc(sizeof(struct goldfish_dma_context), GFP_KERNEL);
952 if (!dma) {
953 ERR("Could not allocate DMA context info!");
954 return;
955 }
956 dma->dma_size = size;
957 mutex_init(&dma->mutex_lock);
958
959 mutex_lock(&pipe->lock);
960 pipe->dma = dma;
961 pipe->dma->pdev_dev = pipe->dev->pdev_dev;
962 mutex_unlock(&pipe->lock);
963 }
964
goldfish_pipe_dma_acquire_lock(struct goldfish_pipe * pipe)965 static int goldfish_pipe_dma_acquire_lock(struct goldfish_pipe *pipe) {
966 if (pipe->closing) return 0;
967 smp_mb();
968 if (pipe->dma && pipe->dma->locked) {
969 set_bit(BIT_WAKE_ON_UNLOCK_DMA, &pipe->flags);
970 return goldfish_pipe_wait_event(BIT_WAKE_ON_UNLOCK_DMA, pipe);
971 } else if (pipe->dma) {
972 pipe->dma->locked = true;
973 } else {
974 ERR("No dma context for this pipe!");
975 return -EINVAL;
976 }
977 return 0;
978 }
979
goldfish_dma_ioctl(struct file * file,unsigned int cmd,unsigned long arg)980 static long goldfish_dma_ioctl(struct file *file,
981 unsigned int cmd,
982 unsigned long arg)
983 {
984 struct goldfish_pipe *pipe;
985 struct goldfish_dma_context *dma;
986 struct goldfish_dma_ioctl_info ioctl_data;
987 int ret = 0;
988
989 DPRINT("%s: call.", __FUNCTION__);
990 pipe = (struct goldfish_pipe *)(file->private_data);
991
992 if (pipe->closing) return -ENOTTY;
993
994 DPRINT("%s: get dma ptr.", __FUNCTION__);
995 dma = pipe->dma;
996 DPRINT("%s: continuing", __FUNCTION__);
997
998 if (copy_from_user(&ioctl_data, (void __user *)arg, sizeof(ioctl_data))) {
999 return -EFAULT;
1000 }
1001
1002 DPRINT("%s: copied ioctl data from user", __FUNCTION__);
1003
1004 switch (cmd) {
1005 case GOLDFISH_DMA_IOC_LOCK:
1006 DPRINT("LOCK_DMA for pipe 0x%p\n", pipe);
1007 ret = goldfish_pipe_dma_acquire_lock(pipe);
1008 if (ret == 0) {
1009 DPRINT("acquired lock, proceeding for pipe 0x%p\n", pipe);
1010 }
1011 return ret;
1012 case GOLDFISH_DMA_IOC_UNLOCK:
1013 DPRINT("UNLOCK_DMA for pipe 0x%p\n", pipe);
1014 goldfish_pipe_dma_clear_lock(pipe);
1015 wake_up_interruptible(&pipe->wake_queue);
1016 return 0;
1017 case GOLDFISH_DMA_IOC_GETOFF:
1018 DPRINT("DMA_GETOFF for pipe 0x%p\n", pipe);
1019 ioctl_data.phys_begin = dma->phys_begin;
1020 if (copy_to_user((void __user *)arg, &ioctl_data, sizeof(ioctl_data))) {
1021 return -EFAULT;
1022 }
1023 DPRINT("GOLDFISH_DMA_IOC_GETOFF: return 0x%llx", dma->phys_begin);
1024 return 0;
1025 case GOLDFISH_DMA_IOC_CREATE_REGION:
1026 DPRINT("DMA_CREATE_REGION for pipe 0x%p\n", pipe);
1027 if (!is_page_size_multiple(ioctl_data.size)) {
1028 ERR("DMA_CREATE_REGION: %llu not a multiple of page size!\n",
1029 ioctl_data.size);
1030 return -EINVAL;
1031 }
1032 goldfish_pipe_dma_create_region(pipe, ioctl_data.size);
1033 return 0;
1034 default:
1035 return -ENOTTY;
1036 }
1037 }
1038
1039 static const struct file_operations goldfish_pipe_fops = {
1040 .owner = THIS_MODULE,
1041 .read = goldfish_pipe_read,
1042 .write = goldfish_pipe_write,
1043 .poll = goldfish_pipe_poll,
1044 .open = goldfish_pipe_open,
1045 .release = goldfish_pipe_release,
1046 /* DMA-related operations */
1047 .mmap = goldfish_dma_mmap,
1048 .unlocked_ioctl = goldfish_dma_ioctl,
1049 .compat_ioctl = goldfish_dma_ioctl,
1050 };
1051
1052 static struct miscdevice goldfish_pipe_miscdev = {
1053 .minor = MISC_DYNAMIC_MINOR,
1054 .name = "goldfish_pipe",
1055 .fops = &goldfish_pipe_fops,
1056 };
1057
goldfish_pipe_device_init_v2(struct platform_device * pdev)1058 static int goldfish_pipe_device_init_v2(struct platform_device *pdev)
1059 {
1060 char *page;
1061 struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1062 struct device *pdev_dev = &pdev->dev;
1063 int err = devm_request_irq(pdev_dev, dev->irq, goldfish_pipe_interrupt,
1064 IRQF_SHARED, "goldfish_pipe", dev);
1065 if (err) {
1066 dev_err(pdev_dev, "unable to allocate IRQ for v2\n");
1067 return err;
1068 }
1069
1070 err = misc_register(&goldfish_pipe_miscdev);
1071 if (err) {
1072 dev_err(pdev_dev, "unable to register v2 device\n");
1073 return err;
1074 }
1075
1076 dev->pdev_dev = pdev_dev;
1077 dev->first_signalled_pipe = NULL;
1078 dev->pipes_capacity = INITIAL_PIPES_CAPACITY;
1079 dev->pipes = kcalloc(dev->pipes_capacity, sizeof(*dev->pipes), GFP_KERNEL);
1080 if (!dev->pipes)
1081 return -ENOMEM;
1082
1083 /*
1084 * We're going to pass two buffers, open_command_params and
1085 * signalled_pipe_buffers, to the host. This means each of those buffers
1086 * needs to be contained in a single physical page. The easiest choice is
1087 * to just allocate a page and place the buffers in it.
1088 */
1089 BUG_ON(sizeof(*dev->buffers) > PAGE_SIZE);
1090 page = (char *)__get_free_page(GFP_KERNEL);
1091 if (!page) {
1092 kfree(dev->pipes);
1093 return -ENOMEM;
1094 }
1095 dev->buffers = (struct goldfish_pipe_dev_buffers *)page;
1096
1097 /* Send the buffer addresses to the host */
1098 {
1099 u64 paddr = __pa(&dev->buffers->signalled_pipe_buffers);
1100 writel((u32)(unsigned long)(paddr >> 32), dev->base + PIPE_REG_SIGNAL_BUFFER_HIGH);
1101 writel((u32)(unsigned long)paddr, dev->base + PIPE_REG_SIGNAL_BUFFER);
1102 writel((u32)MAX_SIGNALLED_PIPES, dev->base + PIPE_REG_SIGNAL_BUFFER_COUNT);
1103
1104 paddr = __pa(&dev->buffers->open_command_params);
1105 writel((u32)(unsigned long)(paddr >> 32), dev->base + PIPE_REG_OPEN_BUFFER_HIGH);
1106 writel((u32)(unsigned long)paddr, dev->base + PIPE_REG_OPEN_BUFFER);
1107 }
1108
1109 /* Perform initial checks for pipe DMA */
1110 BUG_ON(sizeof(dma_addr_t) > sizeof(u64)); /* Only support up to 64-bit dma_addr_t's */
1111 return 0;
1112 }
1113
goldfish_pipe_device_deinit_v2(struct platform_device * pdev)1114 static void goldfish_pipe_device_deinit_v2(struct platform_device *pdev) {
1115 struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1116 misc_deregister(&goldfish_pipe_miscdev);
1117 kfree(dev->pipes);
1118 free_page((unsigned long)dev->buffers);
1119 }
1120
goldfish_pipe_probe(struct platform_device * pdev)1121 static int goldfish_pipe_probe(struct platform_device *pdev)
1122 {
1123 int err;
1124 struct resource *r;
1125 struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1126 struct device *pdev_dev = &pdev->dev;
1127
1128 BUG_ON(sizeof(struct goldfish_pipe_command) > PAGE_SIZE);
1129
1130 /* not thread safe, but this should not happen */
1131 WARN_ON(dev->base != NULL);
1132
1133 spin_lock_init(&dev->lock);
1134
1135 pdev_dev->coherent_dma_mask = DMA_BIT_MASK(48);
1136 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1137 if (r == NULL || resource_size(r) < PAGE_SIZE) {
1138 dev_err(pdev_dev, "can't allocate i/o page\n");
1139 return -EINVAL;
1140 }
1141 dev->base = devm_ioremap(pdev_dev, r->start, PAGE_SIZE);
1142 if (dev->base == NULL) {
1143 dev_err(pdev_dev, "ioremap failed\n");
1144 return -EINVAL;
1145 }
1146
1147 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1148 if (r == NULL) {
1149 err = -EINVAL;
1150 goto error;
1151 }
1152 dev->irq = r->start;
1153
1154 /*
1155 * Exchange the versions with the host device
1156 *
1157 * Note: v1 driver used to not report its version, so we write it before
1158 * reading device version back: this allows the host implementation to
1159 * detect the old driver (if there was no version write before read).
1160 */
1161 writel((u32)PIPE_DRIVER_VERSION, dev->base + PIPE_REG_VERSION);
1162 dev->version = readl(dev->base + PIPE_REG_VERSION);
1163 if (dev->version < PIPE_CURRENT_DEVICE_VERSION) {
1164 /* initialize the old device version */
1165 err = goldfish_pipe_device_init_v1(pdev);
1166 } else {
1167 /* Host device supports the new interface */
1168 err = goldfish_pipe_device_init_v2(pdev);
1169 }
1170 if (!err)
1171 return 0;
1172
1173 error:
1174 dev->base = NULL;
1175 return err;
1176 }
1177
goldfish_pipe_remove(struct platform_device * pdev)1178 static int goldfish_pipe_remove(struct platform_device *pdev)
1179 {
1180 struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1181 if (dev->version < PIPE_CURRENT_DEVICE_VERSION)
1182 goldfish_pipe_device_deinit_v1(pdev);
1183 else
1184 goldfish_pipe_device_deinit_v2(pdev);
1185 dev->base = NULL;
1186 return 0;
1187 }
1188
1189 static const struct acpi_device_id goldfish_pipe_acpi_match[] = {
1190 { "GFSH0003", 0 },
1191 { },
1192 };
1193 MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match);
1194
1195 static const struct of_device_id goldfish_pipe_of_match[] = {
1196 { .compatible = "generic,android-pipe", },
1197 {},
1198 };
1199 MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match);
1200
1201 static struct platform_driver goldfish_pipe_driver = {
1202 .probe = goldfish_pipe_probe,
1203 .remove = goldfish_pipe_remove,
1204 .driver = {
1205 .name = "goldfish_pipe",
1206 .owner = THIS_MODULE,
1207 .of_match_table = goldfish_pipe_of_match,
1208 .acpi_match_table = ACPI_PTR(goldfish_pipe_acpi_match),
1209 }
1210 };
1211
1212 module_platform_driver(goldfish_pipe_driver);
1213 MODULE_AUTHOR("David Turner <digit@google.com>");
1214 MODULE_LICENSE("GPL");
1215