nouveau_usif.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright 2014 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include "nouveau_drv.h"
  25. #include "nouveau_usif.h"
  26. #include "nouveau_abi16.h"
  27. #include <nvif/notify.h>
  28. #include <nvif/unpack.h>
  29. #include <nvif/client.h>
  30. #include <nvif/event.h>
  31. #include <nvif/ioctl.h>
  32. struct usif_notify_p {
  33. struct drm_pending_event base;
  34. struct {
  35. struct drm_event base;
  36. u8 data[];
  37. } e;
  38. };
  39. struct usif_notify {
  40. struct list_head head;
  41. atomic_t enabled;
  42. u32 handle;
  43. u16 reply;
  44. u8 route;
  45. u64 token;
  46. struct usif_notify_p *p;
  47. };
  48. static inline struct usif_notify *
  49. usif_notify_find(struct drm_file *filp, u32 handle)
  50. {
  51. struct nouveau_cli *cli = nouveau_cli(filp);
  52. struct usif_notify *ntfy;
  53. list_for_each_entry(ntfy, &cli->notifys, head) {
  54. if (ntfy->handle == handle)
  55. return ntfy;
  56. }
  57. return NULL;
  58. }
  59. static inline void
  60. usif_notify_dtor(struct usif_notify *ntfy)
  61. {
  62. list_del(&ntfy->head);
  63. kfree(ntfy);
  64. }
  65. int
  66. usif_notify(const void *header, u32 length, const void *data, u32 size)
  67. {
  68. struct usif_notify *ntfy = NULL;
  69. const union {
  70. struct nvif_notify_rep_v0 v0;
  71. } *rep = header;
  72. struct drm_device *dev;
  73. struct drm_file *filp;
  74. unsigned long flags;
  75. if (length == sizeof(rep->v0) && rep->v0.version == 0) {
  76. if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token)))
  77. return NVIF_NOTIFY_DROP;
  78. BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF);
  79. } else
  80. if (WARN_ON(1))
  81. return NVIF_NOTIFY_DROP;
  82. if (WARN_ON(!ntfy->p || ntfy->reply != (length + size)))
  83. return NVIF_NOTIFY_DROP;
  84. filp = ntfy->p->base.file_priv;
  85. dev = filp->minor->dev;
  86. memcpy(&ntfy->p->e.data[0], header, length);
  87. memcpy(&ntfy->p->e.data[length], data, size);
  88. switch (rep->v0.version) {
  89. case 0: {
  90. struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data;
  91. rep->route = ntfy->route;
  92. rep->token = ntfy->token;
  93. }
  94. break;
  95. default:
  96. BUG();
  97. break;
  98. }
  99. spin_lock_irqsave(&dev->event_lock, flags);
  100. if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) {
  101. list_add_tail(&ntfy->p->base.link, &filp->event_list);
  102. filp->event_space -= ntfy->p->e.base.length;
  103. }
  104. wake_up_interruptible(&filp->event_wait);
  105. spin_unlock_irqrestore(&dev->event_lock, flags);
  106. atomic_set(&ntfy->enabled, 0);
  107. return NVIF_NOTIFY_DROP;
  108. }
  109. static int
  110. usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  111. {
  112. struct nouveau_cli *cli = nouveau_cli(f);
  113. struct nvif_client *client = &cli->base;
  114. union {
  115. struct nvif_ioctl_ntfy_new_v0 v0;
  116. } *args = data;
  117. union {
  118. struct nvif_notify_req_v0 v0;
  119. } *req;
  120. struct usif_notify *ntfy;
  121. int ret = -ENOSYS;
  122. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  123. if (usif_notify_find(f, args->v0.index))
  124. return -EEXIST;
  125. } else
  126. return ret;
  127. req = data;
  128. ret = -ENOSYS;
  129. if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
  130. return -ENOMEM;
  131. atomic_set(&ntfy->enabled, 0);
  132. if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
  133. ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
  134. ntfy->route = req->v0.route;
  135. ntfy->token = req->v0.token;
  136. req->v0.route = NVDRM_NOTIFY_USIF;
  137. req->v0.token = (unsigned long)(void *)ntfy;
  138. ret = nvif_client_ioctl(client, argv, argc);
  139. req->v0.token = ntfy->token;
  140. req->v0.route = ntfy->route;
  141. ntfy->handle = args->v0.index;
  142. }
  143. if (ret == 0)
  144. list_add(&ntfy->head, &cli->notifys);
  145. if (ret)
  146. kfree(ntfy);
  147. return ret;
  148. }
  149. static int
  150. usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  151. {
  152. struct nouveau_cli *cli = nouveau_cli(f);
  153. struct nvif_client *client = &cli->base;
  154. union {
  155. struct nvif_ioctl_ntfy_del_v0 v0;
  156. } *args = data;
  157. struct usif_notify *ntfy;
  158. int ret = -ENOSYS;
  159. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  160. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  161. return -ENOENT;
  162. } else
  163. return ret;
  164. ret = nvif_client_ioctl(client, argv, argc);
  165. if (ret == 0)
  166. usif_notify_dtor(ntfy);
  167. return ret;
  168. }
  169. static int
  170. usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  171. {
  172. struct nouveau_cli *cli = nouveau_cli(f);
  173. struct nvif_client *client = &cli->base;
  174. union {
  175. struct nvif_ioctl_ntfy_del_v0 v0;
  176. } *args = data;
  177. struct usif_notify *ntfy;
  178. int ret = -ENOSYS;
  179. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  180. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  181. return -ENOENT;
  182. } else
  183. return ret;
  184. if (atomic_xchg(&ntfy->enabled, 1))
  185. return 0;
  186. ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
  187. if (ret = -ENOMEM, !ntfy->p)
  188. goto done;
  189. ntfy->p->base.event = &ntfy->p->e.base;
  190. ntfy->p->base.file_priv = f;
  191. ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
  192. ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
  193. ret = nvif_client_ioctl(client, argv, argc);
  194. done:
  195. if (ret) {
  196. atomic_set(&ntfy->enabled, 0);
  197. kfree(ntfy->p);
  198. }
  199. return ret;
  200. }
  201. static int
  202. usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  203. {
  204. struct nouveau_cli *cli = nouveau_cli(f);
  205. struct nvif_client *client = &cli->base;
  206. union {
  207. struct nvif_ioctl_ntfy_put_v0 v0;
  208. } *args = data;
  209. struct usif_notify *ntfy;
  210. int ret = -ENOSYS;
  211. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  212. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  213. return -ENOENT;
  214. } else
  215. return ret;
  216. ret = nvif_client_ioctl(client, argv, argc);
  217. if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
  218. kfree(ntfy->p);
  219. return ret;
  220. }
  221. struct usif_object {
  222. struct list_head head;
  223. struct list_head ntfy;
  224. u8 route;
  225. u64 token;
  226. };
  227. static void
  228. usif_object_dtor(struct usif_object *object)
  229. {
  230. list_del(&object->head);
  231. kfree(object);
  232. }
  233. static int
  234. usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  235. {
  236. struct nouveau_cli *cli = nouveau_cli(f);
  237. struct nvif_client *client = &cli->base;
  238. union {
  239. struct nvif_ioctl_new_v0 v0;
  240. } *args = data;
  241. struct usif_object *object;
  242. int ret = -ENOSYS;
  243. if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
  244. return -ENOMEM;
  245. list_add(&object->head, &cli->objects);
  246. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  247. object->route = args->v0.route;
  248. object->token = args->v0.token;
  249. args->v0.route = NVDRM_OBJECT_USIF;
  250. args->v0.token = (unsigned long)(void *)object;
  251. ret = nvif_client_ioctl(client, argv, argc);
  252. args->v0.token = object->token;
  253. args->v0.route = object->route;
  254. }
  255. if (ret)
  256. usif_object_dtor(object);
  257. return ret;
  258. }
  259. int
  260. usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
  261. {
  262. struct nouveau_cli *cli = nouveau_cli(filp);
  263. struct nvif_client *client = &cli->base;
  264. void *data = kmalloc(argc, GFP_KERNEL);
  265. u32 size = argc;
  266. union {
  267. struct nvif_ioctl_v0 v0;
  268. } *argv = data;
  269. struct usif_object *object;
  270. u8 owner;
  271. int ret;
  272. if (ret = -ENOMEM, !argv)
  273. goto done;
  274. if (ret = -EFAULT, copy_from_user(argv, user, size))
  275. goto done;
  276. if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) {
  277. /* block access to objects not created via this interface */
  278. owner = argv->v0.owner;
  279. if (argv->v0.object == 0ULL &&
  280. argv->v0.type != NVIF_IOCTL_V0_DEL)
  281. argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */
  282. else
  283. argv->v0.owner = NVDRM_OBJECT_USIF;
  284. } else
  285. goto done;
  286. /* USIF slightly abuses some return-only ioctl members in order
  287. * to provide interoperability with the older ABI16 objects
  288. */
  289. mutex_lock(&cli->mutex);
  290. if (argv->v0.route) {
  291. if (ret = -EINVAL, argv->v0.route == 0xff)
  292. ret = nouveau_abi16_usif(filp, argv, argc);
  293. if (ret) {
  294. mutex_unlock(&cli->mutex);
  295. goto done;
  296. }
  297. }
  298. switch (argv->v0.type) {
  299. case NVIF_IOCTL_V0_NEW:
  300. ret = usif_object_new(filp, data, size, argv, argc);
  301. break;
  302. case NVIF_IOCTL_V0_NTFY_NEW:
  303. ret = usif_notify_new(filp, data, size, argv, argc);
  304. break;
  305. case NVIF_IOCTL_V0_NTFY_DEL:
  306. ret = usif_notify_del(filp, data, size, argv, argc);
  307. break;
  308. case NVIF_IOCTL_V0_NTFY_GET:
  309. ret = usif_notify_get(filp, data, size, argv, argc);
  310. break;
  311. case NVIF_IOCTL_V0_NTFY_PUT:
  312. ret = usif_notify_put(filp, data, size, argv, argc);
  313. break;
  314. default:
  315. ret = nvif_client_ioctl(client, argv, argc);
  316. break;
  317. }
  318. if (argv->v0.route == NVDRM_OBJECT_USIF) {
  319. object = (void *)(unsigned long)argv->v0.token;
  320. argv->v0.route = object->route;
  321. argv->v0.token = object->token;
  322. if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
  323. list_del(&object->head);
  324. kfree(object);
  325. }
  326. } else {
  327. argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
  328. argv->v0.token = 0;
  329. }
  330. argv->v0.owner = owner;
  331. mutex_unlock(&cli->mutex);
  332. if (copy_to_user(user, argv, argc))
  333. ret = -EFAULT;
  334. done:
  335. kfree(argv);
  336. return ret;
  337. }
  338. void
  339. usif_client_fini(struct nouveau_cli *cli)
  340. {
  341. struct usif_object *object, *otemp;
  342. struct usif_notify *notify, *ntemp;
  343. list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
  344. usif_notify_dtor(notify);
  345. }
  346. list_for_each_entry_safe(object, otemp, &cli->objects, head) {
  347. usif_object_dtor(object);
  348. }
  349. }
  350. void
  351. usif_client_init(struct nouveau_cli *cli)
  352. {
  353. INIT_LIST_HEAD(&cli->objects);
  354. INIT_LIST_HEAD(&cli->notifys);
  355. }