goldfish_pipe.c 26 KB

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