nouveau_usif.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 "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_ON(1);
  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->base.pid = current->pid;
  192. ntfy->p->base.destroy =(void(*)(struct drm_pending_event *))kfree;
  193. ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
  194. ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
  195. ret = nvif_client_ioctl(client, argv, argc);
  196. done:
  197. if (ret) {
  198. atomic_set(&ntfy->enabled, 0);
  199. kfree(ntfy->p);
  200. }
  201. return ret;
  202. }
  203. static int
  204. usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  205. {
  206. struct nouveau_cli *cli = nouveau_cli(f);
  207. struct nvif_client *client = &cli->base;
  208. union {
  209. struct nvif_ioctl_ntfy_put_v0 v0;
  210. } *args = data;
  211. struct usif_notify *ntfy;
  212. int ret = -ENOSYS;
  213. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  214. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  215. return -ENOENT;
  216. } else
  217. return ret;
  218. ret = nvif_client_ioctl(client, argv, argc);
  219. if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
  220. kfree(ntfy->p);
  221. return ret;
  222. }
  223. struct usif_object {
  224. struct list_head head;
  225. struct list_head ntfy;
  226. u8 route;
  227. u64 token;
  228. };
  229. static void
  230. usif_object_dtor(struct usif_object *object)
  231. {
  232. list_del(&object->head);
  233. kfree(object);
  234. }
  235. static int
  236. usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  237. {
  238. struct nouveau_cli *cli = nouveau_cli(f);
  239. struct nvif_client *client = &cli->base;
  240. union {
  241. struct nvif_ioctl_new_v0 v0;
  242. } *args = data;
  243. struct usif_object *object;
  244. int ret = -ENOSYS;
  245. if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
  246. return -ENOMEM;
  247. list_add(&object->head, &cli->objects);
  248. if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
  249. object->route = args->v0.route;
  250. object->token = args->v0.token;
  251. args->v0.route = NVDRM_OBJECT_USIF;
  252. args->v0.token = (unsigned long)(void *)object;
  253. ret = nvif_client_ioctl(client, argv, argc);
  254. args->v0.token = object->token;
  255. args->v0.route = object->route;
  256. }
  257. if (ret)
  258. usif_object_dtor(object);
  259. return ret;
  260. }
  261. int
  262. usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
  263. {
  264. struct nouveau_cli *cli = nouveau_cli(filp);
  265. struct nvif_client *client = &cli->base;
  266. void *data = kmalloc(argc, GFP_KERNEL);
  267. u32 size = argc;
  268. union {
  269. struct nvif_ioctl_v0 v0;
  270. } *argv = data;
  271. struct usif_object *object;
  272. u8 owner;
  273. int ret;
  274. if (ret = -ENOMEM, !argv)
  275. goto done;
  276. if (ret = -EFAULT, copy_from_user(argv, user, size))
  277. goto done;
  278. if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) {
  279. /* block access to objects not created via this interface */
  280. owner = argv->v0.owner;
  281. if (argv->v0.object == 0ULL)
  282. argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */
  283. else
  284. argv->v0.owner = NVDRM_OBJECT_USIF;
  285. } else
  286. goto done;
  287. /* USIF slightly abuses some return-only ioctl members in order
  288. * to provide interoperability with the older ABI16 objects
  289. */
  290. mutex_lock(&cli->mutex);
  291. if (argv->v0.route) {
  292. if (ret = -EINVAL, argv->v0.route == 0xff)
  293. ret = nouveau_abi16_usif(filp, argv, argc);
  294. if (ret) {
  295. mutex_unlock(&cli->mutex);
  296. goto done;
  297. }
  298. }
  299. switch (argv->v0.type) {
  300. case NVIF_IOCTL_V0_NEW:
  301. ret = usif_object_new(filp, data, size, argv, argc);
  302. break;
  303. case NVIF_IOCTL_V0_NTFY_NEW:
  304. ret = usif_notify_new(filp, data, size, argv, argc);
  305. break;
  306. case NVIF_IOCTL_V0_NTFY_DEL:
  307. ret = usif_notify_del(filp, data, size, argv, argc);
  308. break;
  309. case NVIF_IOCTL_V0_NTFY_GET:
  310. ret = usif_notify_get(filp, data, size, argv, argc);
  311. break;
  312. case NVIF_IOCTL_V0_NTFY_PUT:
  313. ret = usif_notify_put(filp, data, size, argv, argc);
  314. break;
  315. default:
  316. ret = nvif_client_ioctl(client, argv, argc);
  317. break;
  318. }
  319. if (argv->v0.route == NVDRM_OBJECT_USIF) {
  320. object = (void *)(unsigned long)argv->v0.token;
  321. argv->v0.route = object->route;
  322. argv->v0.token = object->token;
  323. if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
  324. list_del(&object->head);
  325. kfree(object);
  326. }
  327. } else {
  328. argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
  329. argv->v0.token = 0;
  330. }
  331. argv->v0.owner = owner;
  332. mutex_unlock(&cli->mutex);
  333. if (copy_to_user(user, argv, argc))
  334. ret = -EFAULT;
  335. done:
  336. kfree(argv);
  337. return ret;
  338. }
  339. void
  340. usif_client_fini(struct nouveau_cli *cli)
  341. {
  342. struct usif_object *object, *otemp;
  343. struct usif_notify *notify, *ntemp;
  344. list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
  345. usif_notify_dtor(notify);
  346. }
  347. list_for_each_entry_safe(object, otemp, &cli->objects, head) {
  348. usif_object_dtor(object);
  349. }
  350. }
  351. void
  352. usif_client_init(struct nouveau_cli *cli)
  353. {
  354. INIT_LIST_HEAD(&cli->objects);
  355. INIT_LIST_HEAD(&cli->notifys);
  356. }