mc-request.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Media device request objects
  4. *
  5. * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. * Copyright (C) 2018 Intel Corporation
  7. * Copyright (C) 2018 Google, Inc.
  8. *
  9. * Author: Hans Verkuil <hverkuil@kernel.org>
  10. * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
  11. */
  12. #include <linux/anon_inodes.h>
  13. #include <linux/file.h>
  14. #include <linux/refcount.h>
  15. #include <media/media-device.h>
  16. #include <media/media-request.h>
  17. static const char * const request_state[] = {
  18. [MEDIA_REQUEST_STATE_IDLE] = "idle",
  19. [MEDIA_REQUEST_STATE_VALIDATING] = "validating",
  20. [MEDIA_REQUEST_STATE_QUEUED] = "queued",
  21. [MEDIA_REQUEST_STATE_COMPLETE] = "complete",
  22. [MEDIA_REQUEST_STATE_CLEANING] = "cleaning",
  23. [MEDIA_REQUEST_STATE_UPDATING] = "updating",
  24. };
  25. static const char *
  26. media_request_state_str(enum media_request_state state)
  27. {
  28. BUILD_BUG_ON(ARRAY_SIZE(request_state) != NR_OF_MEDIA_REQUEST_STATE);
  29. if (WARN_ON(state >= ARRAY_SIZE(request_state)))
  30. return "invalid";
  31. return request_state[state];
  32. }
  33. static void media_request_clean(struct media_request *req)
  34. {
  35. struct media_request_object *obj, *obj_safe;
  36. /* Just a sanity check. No other code path is allowed to change this. */
  37. WARN_ON(req->state != MEDIA_REQUEST_STATE_CLEANING);
  38. WARN_ON(req->updating_count);
  39. WARN_ON(req->access_count);
  40. list_for_each_entry_safe(obj, obj_safe, &req->objects, list) {
  41. media_request_object_unbind(obj);
  42. media_request_object_put(obj);
  43. }
  44. req->updating_count = 0;
  45. req->access_count = 0;
  46. WARN_ON(req->num_incomplete_objects);
  47. req->num_incomplete_objects = 0;
  48. req->manual_completion = false;
  49. wake_up_interruptible_all(&req->poll_wait);
  50. }
  51. static void media_request_release(struct kref *kref)
  52. {
  53. struct media_request *req =
  54. container_of(kref, struct media_request, kref);
  55. struct media_device *mdev = req->mdev;
  56. dev_dbg(mdev->dev, "request: release %s\n", req->debug_str);
  57. /* No other users, no need for a spinlock */
  58. req->state = MEDIA_REQUEST_STATE_CLEANING;
  59. media_request_clean(req);
  60. if (mdev->ops->req_free)
  61. mdev->ops->req_free(req);
  62. else
  63. kfree(req);
  64. atomic_dec(&mdev->num_requests);
  65. }
  66. void media_request_put(struct media_request *req)
  67. {
  68. kref_put(&req->kref, media_request_release);
  69. }
  70. EXPORT_SYMBOL_GPL(media_request_put);
  71. static int media_request_close(struct inode *inode, struct file *filp)
  72. {
  73. struct media_request *req = filp->private_data;
  74. media_request_put(req);
  75. return 0;
  76. }
  77. static __poll_t media_request_poll(struct file *filp,
  78. struct poll_table_struct *wait)
  79. {
  80. struct media_request *req = filp->private_data;
  81. unsigned long flags;
  82. __poll_t ret = 0;
  83. if (!(poll_requested_events(wait) & EPOLLPRI))
  84. return 0;
  85. poll_wait(filp, &req->poll_wait, wait);
  86. spin_lock_irqsave(&req->lock, flags);
  87. if (req->state == MEDIA_REQUEST_STATE_COMPLETE) {
  88. ret = EPOLLPRI;
  89. goto unlock;
  90. }
  91. if (req->state != MEDIA_REQUEST_STATE_QUEUED) {
  92. ret = EPOLLERR;
  93. goto unlock;
  94. }
  95. unlock:
  96. spin_unlock_irqrestore(&req->lock, flags);
  97. return ret;
  98. }
  99. static long media_request_ioctl_queue(struct media_request *req)
  100. {
  101. struct media_device *mdev = req->mdev;
  102. enum media_request_state state;
  103. unsigned long flags;
  104. int ret;
  105. dev_dbg(mdev->dev, "request: queue %s\n", req->debug_str);
  106. /*
  107. * Ensure the request that is validated will be the one that gets queued
  108. * next by serialising the queueing process. This mutex is also used
  109. * to serialize with canceling a vb2 queue and with setting values such
  110. * as controls in a request.
  111. */
  112. mutex_lock(&mdev->req_queue_mutex);
  113. media_request_get(req);
  114. spin_lock_irqsave(&req->lock, flags);
  115. if (req->state == MEDIA_REQUEST_STATE_IDLE)
  116. req->state = MEDIA_REQUEST_STATE_VALIDATING;
  117. state = req->state;
  118. spin_unlock_irqrestore(&req->lock, flags);
  119. if (state != MEDIA_REQUEST_STATE_VALIDATING) {
  120. dev_dbg(mdev->dev,
  121. "request: unable to queue %s, request in state %s\n",
  122. req->debug_str, media_request_state_str(state));
  123. media_request_put(req);
  124. mutex_unlock(&mdev->req_queue_mutex);
  125. return -EBUSY;
  126. }
  127. ret = mdev->ops->req_validate(req);
  128. /*
  129. * If the req_validate was successful, then we mark the state as QUEUED
  130. * and call req_queue. The reason we set the state first is that this
  131. * allows req_queue to unbind or complete the queued objects in case
  132. * they are immediately 'consumed'. State changes from QUEUED to another
  133. * state can only happen if either the driver changes the state or if
  134. * the user cancels the vb2 queue. The driver can only change the state
  135. * after each object is queued through the req_queue op (and note that
  136. * that op cannot fail), so setting the state to QUEUED up front is
  137. * safe.
  138. *
  139. * The other reason for changing the state is if the vb2 queue is
  140. * canceled, and that uses the req_queue_mutex which is still locked
  141. * while req_queue is called, so that's safe as well.
  142. */
  143. spin_lock_irqsave(&req->lock, flags);
  144. req->state = ret ? MEDIA_REQUEST_STATE_IDLE
  145. : MEDIA_REQUEST_STATE_QUEUED;
  146. spin_unlock_irqrestore(&req->lock, flags);
  147. if (!ret)
  148. mdev->ops->req_queue(req);
  149. mutex_unlock(&mdev->req_queue_mutex);
  150. if (ret) {
  151. dev_dbg(mdev->dev, "request: can't queue %s (%d)\n",
  152. req->debug_str, ret);
  153. media_request_put(req);
  154. }
  155. return ret;
  156. }
  157. static long media_request_ioctl_reinit(struct media_request *req)
  158. {
  159. struct media_device *mdev = req->mdev;
  160. unsigned long flags;
  161. mutex_lock(&mdev->req_queue_mutex);
  162. spin_lock_irqsave(&req->lock, flags);
  163. if (req->state != MEDIA_REQUEST_STATE_IDLE &&
  164. req->state != MEDIA_REQUEST_STATE_COMPLETE) {
  165. dev_dbg(mdev->dev,
  166. "request: %s not in idle or complete state, cannot reinit\n",
  167. req->debug_str);
  168. spin_unlock_irqrestore(&req->lock, flags);
  169. mutex_unlock(&mdev->req_queue_mutex);
  170. return -EBUSY;
  171. }
  172. if (req->access_count) {
  173. dev_dbg(mdev->dev,
  174. "request: %s is being accessed, cannot reinit\n",
  175. req->debug_str);
  176. spin_unlock_irqrestore(&req->lock, flags);
  177. mutex_unlock(&mdev->req_queue_mutex);
  178. return -EBUSY;
  179. }
  180. req->state = MEDIA_REQUEST_STATE_CLEANING;
  181. spin_unlock_irqrestore(&req->lock, flags);
  182. media_request_clean(req);
  183. spin_lock_irqsave(&req->lock, flags);
  184. req->state = MEDIA_REQUEST_STATE_IDLE;
  185. spin_unlock_irqrestore(&req->lock, flags);
  186. mutex_unlock(&mdev->req_queue_mutex);
  187. return 0;
  188. }
  189. static long media_request_ioctl(struct file *filp, unsigned int cmd,
  190. unsigned long arg)
  191. {
  192. struct media_request *req = filp->private_data;
  193. switch (cmd) {
  194. case MEDIA_REQUEST_IOC_QUEUE:
  195. return media_request_ioctl_queue(req);
  196. case MEDIA_REQUEST_IOC_REINIT:
  197. return media_request_ioctl_reinit(req);
  198. default:
  199. return -ENOIOCTLCMD;
  200. }
  201. }
  202. static const struct file_operations request_fops = {
  203. .owner = THIS_MODULE,
  204. .poll = media_request_poll,
  205. .unlocked_ioctl = media_request_ioctl,
  206. #ifdef CONFIG_COMPAT
  207. .compat_ioctl = media_request_ioctl,
  208. #endif /* CONFIG_COMPAT */
  209. .release = media_request_close,
  210. };
  211. struct media_request *
  212. media_request_get_by_fd(struct media_device *mdev, int request_fd)
  213. {
  214. struct media_request *req;
  215. if (!mdev || !mdev->ops ||
  216. !mdev->ops->req_validate || !mdev->ops->req_queue)
  217. return ERR_PTR(-EBADR);
  218. CLASS(fd, f)(request_fd);
  219. if (fd_empty(f))
  220. goto err;
  221. if (fd_file(f)->f_op != &request_fops)
  222. goto err;
  223. req = fd_file(f)->private_data;
  224. if (req->mdev != mdev)
  225. goto err;
  226. /*
  227. * Note: as long as someone has an open filehandle of the request,
  228. * the request can never be released. The fdget() above ensures that
  229. * even if userspace closes the request filehandle, the release()
  230. * fop won't be called, so the media_request_get() always succeeds
  231. * and there is no race condition where the request was released
  232. * before media_request_get() is called.
  233. */
  234. media_request_get(req);
  235. return req;
  236. err:
  237. dev_dbg(mdev->dev, "cannot find request_fd %d\n", request_fd);
  238. return ERR_PTR(-EINVAL);
  239. }
  240. EXPORT_SYMBOL_GPL(media_request_get_by_fd);
  241. int media_request_alloc(struct media_device *mdev, int *alloc_fd)
  242. {
  243. struct media_request *req;
  244. int ret;
  245. /* Either both are NULL or both are non-NULL */
  246. if (WARN_ON(!mdev->ops->req_alloc ^ !mdev->ops->req_free))
  247. return -ENOMEM;
  248. if (mdev->ops->req_alloc)
  249. req = mdev->ops->req_alloc(mdev);
  250. else
  251. req = kzalloc_obj(*req);
  252. if (!req)
  253. return -ENOMEM;
  254. req->mdev = mdev;
  255. req->state = MEDIA_REQUEST_STATE_IDLE;
  256. req->num_incomplete_objects = 0;
  257. req->manual_completion = false;
  258. kref_init(&req->kref);
  259. INIT_LIST_HEAD(&req->objects);
  260. spin_lock_init(&req->lock);
  261. init_waitqueue_head(&req->poll_wait);
  262. req->updating_count = 0;
  263. req->access_count = 0;
  264. FD_PREPARE(fdf, O_CLOEXEC,
  265. anon_inode_getfile("request", &request_fops, NULL,
  266. O_CLOEXEC));
  267. if (fdf.err) {
  268. ret = fdf.err;
  269. goto err_free_req;
  270. }
  271. fd_prepare_file(fdf)->private_data = req;
  272. snprintf(req->debug_str, sizeof(req->debug_str), "%u:%d",
  273. atomic_inc_return(&mdev->request_id), fd_prepare_fd(fdf));
  274. atomic_inc(&mdev->num_requests);
  275. dev_dbg(mdev->dev, "request: allocated %s\n", req->debug_str);
  276. *alloc_fd = fd_publish(fdf);
  277. return 0;
  278. err_free_req:
  279. if (mdev->ops->req_free)
  280. mdev->ops->req_free(req);
  281. else
  282. kfree(req);
  283. return ret;
  284. }
  285. static void media_request_object_release(struct kref *kref)
  286. {
  287. struct media_request_object *obj =
  288. container_of(kref, struct media_request_object, kref);
  289. struct media_request *req = obj->req;
  290. struct media_device *mdev = obj->mdev;
  291. if (WARN_ON(req))
  292. media_request_object_unbind(obj);
  293. obj->ops->release(obj);
  294. atomic_dec(&mdev->num_request_objects);
  295. }
  296. struct media_request_object *
  297. media_request_object_find(struct media_request *req,
  298. const struct media_request_object_ops *ops,
  299. void *priv)
  300. {
  301. struct media_request_object *obj;
  302. struct media_request_object *found = NULL;
  303. unsigned long flags;
  304. if (WARN_ON(!ops || !priv))
  305. return NULL;
  306. spin_lock_irqsave(&req->lock, flags);
  307. list_for_each_entry(obj, &req->objects, list) {
  308. if (obj->ops == ops && obj->priv == priv) {
  309. media_request_object_get(obj);
  310. found = obj;
  311. break;
  312. }
  313. }
  314. spin_unlock_irqrestore(&req->lock, flags);
  315. return found;
  316. }
  317. EXPORT_SYMBOL_GPL(media_request_object_find);
  318. void media_request_object_put(struct media_request_object *obj)
  319. {
  320. kref_put(&obj->kref, media_request_object_release);
  321. }
  322. EXPORT_SYMBOL_GPL(media_request_object_put);
  323. void media_request_object_init(struct media_request_object *obj)
  324. {
  325. obj->ops = NULL;
  326. obj->req = NULL;
  327. obj->priv = NULL;
  328. obj->completed = false;
  329. INIT_LIST_HEAD(&obj->list);
  330. kref_init(&obj->kref);
  331. }
  332. EXPORT_SYMBOL_GPL(media_request_object_init);
  333. int media_request_object_bind(struct media_request *req,
  334. const struct media_request_object_ops *ops,
  335. void *priv, bool is_buffer,
  336. struct media_request_object *obj)
  337. {
  338. unsigned long flags;
  339. int ret = -EBUSY;
  340. if (WARN_ON(!ops->release))
  341. return -EBADR;
  342. spin_lock_irqsave(&req->lock, flags);
  343. if (WARN_ON(req->state != MEDIA_REQUEST_STATE_UPDATING &&
  344. req->state != MEDIA_REQUEST_STATE_QUEUED))
  345. goto unlock;
  346. obj->req = req;
  347. obj->ops = ops;
  348. obj->priv = priv;
  349. obj->mdev = req->mdev;
  350. if (is_buffer)
  351. list_add_tail(&obj->list, &req->objects);
  352. else
  353. list_add(&obj->list, &req->objects);
  354. req->num_incomplete_objects++;
  355. ret = 0;
  356. atomic_inc(&obj->mdev->num_request_objects);
  357. unlock:
  358. spin_unlock_irqrestore(&req->lock, flags);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL_GPL(media_request_object_bind);
  362. void media_request_object_unbind(struct media_request_object *obj)
  363. {
  364. struct media_request *req = obj->req;
  365. unsigned long flags;
  366. bool completed = false;
  367. if (WARN_ON(!req))
  368. return;
  369. spin_lock_irqsave(&req->lock, flags);
  370. list_del(&obj->list);
  371. obj->req = NULL;
  372. if (req->state == MEDIA_REQUEST_STATE_COMPLETE)
  373. goto unlock;
  374. if (WARN_ON(req->state == MEDIA_REQUEST_STATE_VALIDATING))
  375. goto unlock;
  376. if (req->state == MEDIA_REQUEST_STATE_CLEANING) {
  377. if (!obj->completed)
  378. req->num_incomplete_objects--;
  379. goto unlock;
  380. }
  381. if (WARN_ON(!req->num_incomplete_objects))
  382. goto unlock;
  383. req->num_incomplete_objects--;
  384. if (req->state == MEDIA_REQUEST_STATE_QUEUED &&
  385. !req->num_incomplete_objects && !req->manual_completion) {
  386. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  387. completed = true;
  388. wake_up_interruptible_all(&req->poll_wait);
  389. }
  390. unlock:
  391. spin_unlock_irqrestore(&req->lock, flags);
  392. if (obj->ops->unbind)
  393. obj->ops->unbind(obj);
  394. if (completed)
  395. media_request_put(req);
  396. }
  397. EXPORT_SYMBOL_GPL(media_request_object_unbind);
  398. void media_request_object_complete(struct media_request_object *obj)
  399. {
  400. struct media_request *req = obj->req;
  401. unsigned long flags;
  402. bool completed = false;
  403. spin_lock_irqsave(&req->lock, flags);
  404. if (obj->completed)
  405. goto unlock;
  406. obj->completed = true;
  407. if (WARN_ON(!req->num_incomplete_objects) ||
  408. WARN_ON(req->state != MEDIA_REQUEST_STATE_QUEUED))
  409. goto unlock;
  410. if (!--req->num_incomplete_objects && !req->manual_completion) {
  411. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  412. wake_up_interruptible_all(&req->poll_wait);
  413. completed = true;
  414. }
  415. unlock:
  416. spin_unlock_irqrestore(&req->lock, flags);
  417. if (completed)
  418. media_request_put(req);
  419. }
  420. EXPORT_SYMBOL_GPL(media_request_object_complete);
  421. void media_request_manual_complete(struct media_request *req)
  422. {
  423. bool completed = false;
  424. unsigned long flags;
  425. if (WARN_ON_ONCE(!req))
  426. return;
  427. spin_lock_irqsave(&req->lock, flags);
  428. if (WARN_ON_ONCE(!req->manual_completion))
  429. goto unlock;
  430. if (WARN_ON_ONCE(req->state != MEDIA_REQUEST_STATE_QUEUED))
  431. goto unlock;
  432. req->manual_completion = false;
  433. /*
  434. * It is expected that all other objects in this request are
  435. * completed when this function is called. WARN if that is
  436. * not the case.
  437. */
  438. if (!WARN_ON(req->num_incomplete_objects)) {
  439. req->state = MEDIA_REQUEST_STATE_COMPLETE;
  440. wake_up_interruptible_all(&req->poll_wait);
  441. completed = true;
  442. }
  443. unlock:
  444. spin_unlock_irqrestore(&req->lock, flags);
  445. if (completed)
  446. media_request_put(req);
  447. }
  448. EXPORT_SYMBOL_GPL(media_request_manual_complete);