nouveau_usif.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_drm.h"
  25. #include "nouveau_usif.h"
  26. #include <nvif/notify.h>
  27. #include <nvif/unpack.h>
  28. #include <nvif/client.h>
  29. #include <nvif/event.h>
  30. #include <nvif/ioctl.h>
  31. struct usif_notify_p {
  32. struct drm_pending_event base;
  33. struct {
  34. struct drm_event base;
  35. u8 data[];
  36. } e;
  37. };
  38. struct usif_notify {
  39. struct list_head head;
  40. atomic_t enabled;
  41. u32 handle;
  42. u16 reply;
  43. u8 route;
  44. u64 token;
  45. struct usif_notify_p *p;
  46. };
  47. static inline struct usif_notify *
  48. usif_notify_find(struct drm_file *filp, u32 handle)
  49. {
  50. struct nouveau_cli *cli = nouveau_cli(filp);
  51. struct usif_notify *ntfy;
  52. list_for_each_entry(ntfy, &cli->notifys, head) {
  53. if (ntfy->handle == handle)
  54. return ntfy;
  55. }
  56. return NULL;
  57. }
  58. static inline void
  59. usif_notify_dtor(struct usif_notify *ntfy)
  60. {
  61. list_del(&ntfy->head);
  62. kfree(ntfy);
  63. }
  64. int
  65. usif_notify(const void *header, u32 length, const void *data, u32 size)
  66. {
  67. struct usif_notify *ntfy = NULL;
  68. const union {
  69. struct nvif_notify_rep_v0 v0;
  70. } *rep = header;
  71. struct drm_device *dev;
  72. struct drm_file *filp;
  73. unsigned long flags;
  74. if (length == sizeof(rep->v0) && rep->v0.version == 0) {
  75. if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token)))
  76. return NVIF_NOTIFY_DROP;
  77. BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF);
  78. } else
  79. if (WARN_ON(1))
  80. return NVIF_NOTIFY_DROP;
  81. if (WARN_ON(!ntfy->p || ntfy->reply != (length + size)))
  82. return NVIF_NOTIFY_DROP;
  83. filp = ntfy->p->base.file_priv;
  84. dev = filp->minor->dev;
  85. memcpy(&ntfy->p->e.data[0], header, length);
  86. memcpy(&ntfy->p->e.data[length], data, size);
  87. switch (rep->v0.version) {
  88. case 0: {
  89. struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data;
  90. rep->route = ntfy->route;
  91. rep->token = ntfy->token;
  92. }
  93. break;
  94. default:
  95. BUG_ON(1);
  96. break;
  97. }
  98. spin_lock_irqsave(&dev->event_lock, flags);
  99. if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) {
  100. list_add_tail(&ntfy->p->base.link, &filp->event_list);
  101. filp->event_space -= ntfy->p->e.base.length;
  102. }
  103. wake_up_interruptible(&filp->event_wait);
  104. spin_unlock_irqrestore(&dev->event_lock, flags);
  105. atomic_set(&ntfy->enabled, 0);
  106. return NVIF_NOTIFY_DROP;
  107. }
  108. static int
  109. usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  110. {
  111. struct nouveau_cli *cli = nouveau_cli(f);
  112. struct nvif_client *client = &cli->base;
  113. union {
  114. struct nvif_ioctl_ntfy_new_v0 v0;
  115. } *args = data;
  116. union {
  117. struct nvif_notify_req_v0 v0;
  118. } *req;
  119. struct usif_notify *ntfy;
  120. int ret;
  121. if (nvif_unpack(args->v0, 0, 0, true)) {
  122. if (usif_notify_find(f, args->v0.index))
  123. return -EEXIST;
  124. } else
  125. return ret;
  126. req = data;
  127. if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
  128. return -ENOMEM;
  129. atomic_set(&ntfy->enabled, 0);
  130. if (nvif_unpack(req->v0, 0, 0, true)) {
  131. ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
  132. ntfy->route = req->v0.route;
  133. ntfy->token = req->v0.token;
  134. req->v0.route = NVDRM_NOTIFY_USIF;
  135. req->v0.token = (unsigned long)(void *)ntfy;
  136. ret = nvif_client_ioctl(client, argv, argc);
  137. req->v0.token = ntfy->token;
  138. req->v0.route = ntfy->route;
  139. ntfy->handle = args->v0.index;
  140. }
  141. if (ret == 0)
  142. list_add(&ntfy->head, &cli->notifys);
  143. if (ret)
  144. kfree(ntfy);
  145. return ret;
  146. }
  147. static int
  148. usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  149. {
  150. struct nouveau_cli *cli = nouveau_cli(f);
  151. struct nvif_client *client = &cli->base;
  152. union {
  153. struct nvif_ioctl_ntfy_del_v0 v0;
  154. } *args = data;
  155. struct usif_notify *ntfy;
  156. int ret;
  157. if (nvif_unpack(args->v0, 0, 0, true)) {
  158. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  159. return -ENOENT;
  160. } else
  161. return ret;
  162. ret = nvif_client_ioctl(client, argv, argc);
  163. if (ret == 0)
  164. usif_notify_dtor(ntfy);
  165. return ret;
  166. }
  167. static int
  168. usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  169. {
  170. struct nouveau_cli *cli = nouveau_cli(f);
  171. struct nvif_client *client = &cli->base;
  172. union {
  173. struct nvif_ioctl_ntfy_del_v0 v0;
  174. } *args = data;
  175. struct usif_notify *ntfy;
  176. int ret;
  177. if (nvif_unpack(args->v0, 0, 0, true)) {
  178. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  179. return -ENOENT;
  180. } else
  181. return ret;
  182. if (atomic_xchg(&ntfy->enabled, 1))
  183. return 0;
  184. ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
  185. if (ret = -ENOMEM, !ntfy->p)
  186. goto done;
  187. ntfy->p->base.event = &ntfy->p->e.base;
  188. ntfy->p->base.file_priv = f;
  189. ntfy->p->base.pid = current->pid;
  190. ntfy->p->base.destroy =(void(*)(struct drm_pending_event *))kfree;
  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;
  211. if (nvif_unpack(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;
  243. if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
  244. return -ENOMEM;
  245. list_add(&object->head, &cli->objects);
  246. if (nvif_unpack(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 (nvif_unpack(argv->v0, 0, 0, true)) {
  277. /* block access to objects not created via this interface */
  278. owner = argv->v0.owner;
  279. argv->v0.owner = NVDRM_OBJECT_USIF;
  280. } else
  281. goto done;
  282. mutex_lock(&cli->mutex);
  283. switch (argv->v0.type) {
  284. case NVIF_IOCTL_V0_NEW:
  285. /* ... except if we're creating children */
  286. argv->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
  287. ret = usif_object_new(filp, data, size, argv, argc);
  288. break;
  289. case NVIF_IOCTL_V0_NTFY_NEW:
  290. ret = usif_notify_new(filp, data, size, argv, argc);
  291. break;
  292. case NVIF_IOCTL_V0_NTFY_DEL:
  293. ret = usif_notify_del(filp, data, size, argv, argc);
  294. break;
  295. case NVIF_IOCTL_V0_NTFY_GET:
  296. ret = usif_notify_get(filp, data, size, argv, argc);
  297. break;
  298. case NVIF_IOCTL_V0_NTFY_PUT:
  299. ret = usif_notify_put(filp, data, size, argv, argc);
  300. break;
  301. default:
  302. ret = nvif_client_ioctl(client, argv, argc);
  303. break;
  304. }
  305. if (argv->v0.route == NVDRM_OBJECT_USIF) {
  306. object = (void *)(unsigned long)argv->v0.token;
  307. argv->v0.route = object->route;
  308. argv->v0.token = object->token;
  309. if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
  310. list_del(&object->head);
  311. kfree(object);
  312. }
  313. } else {
  314. argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
  315. argv->v0.token = 0;
  316. }
  317. argv->v0.owner = owner;
  318. mutex_unlock(&cli->mutex);
  319. if (copy_to_user(user, argv, argc))
  320. ret = -EFAULT;
  321. done:
  322. kfree(argv);
  323. return ret;
  324. }
  325. void
  326. usif_client_fini(struct nouveau_cli *cli)
  327. {
  328. struct usif_object *object, *otemp;
  329. struct usif_notify *notify, *ntemp;
  330. list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
  331. usif_notify_dtor(notify);
  332. }
  333. list_for_each_entry_safe(object, otemp, &cli->objects, head) {
  334. usif_object_dtor(object);
  335. }
  336. }
  337. void
  338. usif_client_init(struct nouveau_cli *cli)
  339. {
  340. INIT_LIST_HEAD(&cli->objects);
  341. INIT_LIST_HEAD(&cli->notifys);
  342. }