nouveau_usif.c 9.7 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;
  122. if (nvif_unpack(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. if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
  129. return -ENOMEM;
  130. atomic_set(&ntfy->enabled, 0);
  131. if (nvif_unpack(req->v0, 0, 0, true)) {
  132. ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
  133. ntfy->route = req->v0.route;
  134. ntfy->token = req->v0.token;
  135. req->v0.route = NVDRM_NOTIFY_USIF;
  136. req->v0.token = (unsigned long)(void *)ntfy;
  137. ret = nvif_client_ioctl(client, argv, argc);
  138. req->v0.token = ntfy->token;
  139. req->v0.route = ntfy->route;
  140. ntfy->handle = args->v0.index;
  141. }
  142. if (ret == 0)
  143. list_add(&ntfy->head, &cli->notifys);
  144. if (ret)
  145. kfree(ntfy);
  146. return ret;
  147. }
  148. static int
  149. usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  150. {
  151. struct nouveau_cli *cli = nouveau_cli(f);
  152. struct nvif_client *client = &cli->base;
  153. union {
  154. struct nvif_ioctl_ntfy_del_v0 v0;
  155. } *args = data;
  156. struct usif_notify *ntfy;
  157. int ret;
  158. if (nvif_unpack(args->v0, 0, 0, true)) {
  159. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  160. return -ENOENT;
  161. } else
  162. return ret;
  163. ret = nvif_client_ioctl(client, argv, argc);
  164. if (ret == 0)
  165. usif_notify_dtor(ntfy);
  166. return ret;
  167. }
  168. static int
  169. usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  170. {
  171. struct nouveau_cli *cli = nouveau_cli(f);
  172. struct nvif_client *client = &cli->base;
  173. union {
  174. struct nvif_ioctl_ntfy_del_v0 v0;
  175. } *args = data;
  176. struct usif_notify *ntfy;
  177. int ret;
  178. if (nvif_unpack(args->v0, 0, 0, true)) {
  179. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  180. return -ENOENT;
  181. } else
  182. return ret;
  183. if (atomic_xchg(&ntfy->enabled, 1))
  184. return 0;
  185. ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
  186. if (ret = -ENOMEM, !ntfy->p)
  187. goto done;
  188. ntfy->p->base.event = &ntfy->p->e.base;
  189. ntfy->p->base.file_priv = f;
  190. ntfy->p->base.pid = current->pid;
  191. ntfy->p->base.destroy =(void(*)(struct drm_pending_event *))kfree;
  192. ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
  193. ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
  194. ret = nvif_client_ioctl(client, argv, argc);
  195. done:
  196. if (ret) {
  197. atomic_set(&ntfy->enabled, 0);
  198. kfree(ntfy->p);
  199. }
  200. return ret;
  201. }
  202. static int
  203. usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  204. {
  205. struct nouveau_cli *cli = nouveau_cli(f);
  206. struct nvif_client *client = &cli->base;
  207. union {
  208. struct nvif_ioctl_ntfy_put_v0 v0;
  209. } *args = data;
  210. struct usif_notify *ntfy;
  211. int ret;
  212. if (nvif_unpack(args->v0, 0, 0, true)) {
  213. if (!(ntfy = usif_notify_find(f, args->v0.index)))
  214. return -ENOENT;
  215. } else
  216. return ret;
  217. ret = nvif_client_ioctl(client, argv, argc);
  218. if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
  219. kfree(ntfy->p);
  220. return ret;
  221. }
  222. struct usif_object {
  223. struct list_head head;
  224. struct list_head ntfy;
  225. u8 route;
  226. u64 token;
  227. };
  228. static void
  229. usif_object_dtor(struct usif_object *object)
  230. {
  231. list_del(&object->head);
  232. kfree(object);
  233. }
  234. static int
  235. usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
  236. {
  237. struct nouveau_cli *cli = nouveau_cli(f);
  238. struct nvif_client *client = &cli->base;
  239. union {
  240. struct nvif_ioctl_new_v0 v0;
  241. } *args = data;
  242. struct usif_object *object;
  243. int ret;
  244. if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
  245. return -ENOMEM;
  246. list_add(&object->head, &cli->objects);
  247. if (nvif_unpack(args->v0, 0, 0, true)) {
  248. object->route = args->v0.route;
  249. object->token = args->v0.token;
  250. args->v0.route = NVDRM_OBJECT_USIF;
  251. args->v0.token = (unsigned long)(void *)object;
  252. ret = nvif_client_ioctl(client, argv, argc);
  253. args->v0.token = object->token;
  254. args->v0.route = object->route;
  255. }
  256. if (ret)
  257. usif_object_dtor(object);
  258. return ret;
  259. }
  260. int
  261. usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
  262. {
  263. struct nouveau_cli *cli = nouveau_cli(filp);
  264. struct nvif_client *client = &cli->base;
  265. void *data = kmalloc(argc, GFP_KERNEL);
  266. u32 size = argc;
  267. union {
  268. struct nvif_ioctl_v0 v0;
  269. } *argv = data;
  270. struct usif_object *object;
  271. u8 owner;
  272. int ret;
  273. if (ret = -ENOMEM, !argv)
  274. goto done;
  275. if (ret = -EFAULT, copy_from_user(argv, user, size))
  276. goto done;
  277. if (nvif_unpack(argv->v0, 0, 0, true)) {
  278. /* block access to objects not created via this interface */
  279. owner = argv->v0.owner;
  280. if (argv->v0.object == 0ULL &&
  281. argv->v0.type != NVIF_IOCTL_V0_DEL)
  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. }