drm_ioctl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * \file drm_ioctl.c
  3. * IOCTL processing for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Gareth Hughes <gareth@valinux.com>
  7. */
  8. /*
  9. * Created: Fri Jan 8 09:01:26 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include <drm/drmP.h>
  35. #include <drm/drm_core.h>
  36. #include <linux/pci.h>
  37. #include <linux/export.h>
  38. #ifdef CONFIG_X86
  39. #include <asm/mtrr.h>
  40. #endif
  41. /**
  42. * Get the bus id.
  43. *
  44. * \param inode device inode.
  45. * \param file_priv DRM file private.
  46. * \param cmd command.
  47. * \param arg user argument, pointing to a drm_unique structure.
  48. * \return zero on success or a negative number on failure.
  49. *
  50. * Copies the bus id from drm_device::unique into user space.
  51. */
  52. int drm_getunique(struct drm_device *dev, void *data,
  53. struct drm_file *file_priv)
  54. {
  55. struct drm_unique *u = data;
  56. struct drm_master *master = file_priv->master;
  57. if (u->unique_len >= master->unique_len) {
  58. if (copy_to_user(u->unique, master->unique, master->unique_len))
  59. return -EFAULT;
  60. }
  61. u->unique_len = master->unique_len;
  62. return 0;
  63. }
  64. static void
  65. drm_unset_busid(struct drm_device *dev,
  66. struct drm_master *master)
  67. {
  68. kfree(dev->devname);
  69. dev->devname = NULL;
  70. kfree(master->unique);
  71. master->unique = NULL;
  72. master->unique_len = 0;
  73. master->unique_size = 0;
  74. }
  75. /**
  76. * Set the bus id.
  77. *
  78. * \param inode device inode.
  79. * \param file_priv DRM file private.
  80. * \param cmd command.
  81. * \param arg user argument, pointing to a drm_unique structure.
  82. * \return zero on success or a negative number on failure.
  83. *
  84. * Copies the bus id from userspace into drm_device::unique, and verifies that
  85. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  86. * in interface version 1.1 and will return EBUSY when setversion has requested
  87. * version 1.1 or greater.
  88. */
  89. int drm_setunique(struct drm_device *dev, void *data,
  90. struct drm_file *file_priv)
  91. {
  92. struct drm_unique *u = data;
  93. struct drm_master *master = file_priv->master;
  94. int ret;
  95. if (master->unique_len || master->unique)
  96. return -EBUSY;
  97. if (!u->unique_len || u->unique_len > 1024)
  98. return -EINVAL;
  99. if (!dev->driver->bus->set_unique)
  100. return -EINVAL;
  101. ret = dev->driver->bus->set_unique(dev, master, u);
  102. if (ret)
  103. goto err;
  104. return 0;
  105. err:
  106. drm_unset_busid(dev, master);
  107. return ret;
  108. }
  109. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  110. {
  111. struct drm_master *master = file_priv->master;
  112. int ret;
  113. if (master->unique != NULL)
  114. drm_unset_busid(dev, master);
  115. ret = dev->driver->bus->set_busid(dev, master);
  116. if (ret)
  117. goto err;
  118. return 0;
  119. err:
  120. drm_unset_busid(dev, master);
  121. return ret;
  122. }
  123. /**
  124. * Get a mapping information.
  125. *
  126. * \param inode device inode.
  127. * \param file_priv DRM file private.
  128. * \param cmd command.
  129. * \param arg user argument, pointing to a drm_map structure.
  130. *
  131. * \return zero on success or a negative number on failure.
  132. *
  133. * Searches for the mapping with the specified offset and copies its information
  134. * into userspace
  135. */
  136. int drm_getmap(struct drm_device *dev, void *data,
  137. struct drm_file *file_priv)
  138. {
  139. struct drm_map *map = data;
  140. struct drm_map_list *r_list = NULL;
  141. struct list_head *list;
  142. int idx;
  143. int i;
  144. idx = map->offset;
  145. if (idx < 0)
  146. return -EINVAL;
  147. i = 0;
  148. mutex_lock(&dev->struct_mutex);
  149. list_for_each(list, &dev->maplist) {
  150. if (i == idx) {
  151. r_list = list_entry(list, struct drm_map_list, head);
  152. break;
  153. }
  154. i++;
  155. }
  156. if (!r_list || !r_list->map) {
  157. mutex_unlock(&dev->struct_mutex);
  158. return -EINVAL;
  159. }
  160. map->offset = r_list->map->offset;
  161. map->size = r_list->map->size;
  162. map->type = r_list->map->type;
  163. map->flags = r_list->map->flags;
  164. map->handle = (void *)(unsigned long) r_list->user_token;
  165. #ifdef CONFIG_X86
  166. /*
  167. * There appears to be exactly one user of the mtrr index: dritest.
  168. * It's easy enough to keep it working on non-PAT systems.
  169. */
  170. map->mtrr = phys_wc_to_mtrr_index(r_list->map->mtrr);
  171. #else
  172. map->mtrr = -1;
  173. #endif
  174. mutex_unlock(&dev->struct_mutex);
  175. return 0;
  176. }
  177. /**
  178. * Get client information.
  179. *
  180. * \param inode device inode.
  181. * \param file_priv DRM file private.
  182. * \param cmd command.
  183. * \param arg user argument, pointing to a drm_client structure.
  184. *
  185. * \return zero on success or a negative number on failure.
  186. *
  187. * Searches for the client with the specified index and copies its information
  188. * into userspace
  189. */
  190. int drm_getclient(struct drm_device *dev, void *data,
  191. struct drm_file *file_priv)
  192. {
  193. struct drm_client *client = data;
  194. struct drm_file *pt;
  195. int idx;
  196. int i;
  197. idx = client->idx;
  198. i = 0;
  199. mutex_lock(&dev->struct_mutex);
  200. list_for_each_entry(pt, &dev->filelist, lhead) {
  201. if (i++ >= idx) {
  202. client->auth = pt->authenticated;
  203. client->pid = pid_vnr(pt->pid);
  204. client->uid = from_kuid_munged(current_user_ns(), pt->uid);
  205. client->magic = pt->magic;
  206. client->iocs = pt->ioctl_count;
  207. mutex_unlock(&dev->struct_mutex);
  208. return 0;
  209. }
  210. }
  211. mutex_unlock(&dev->struct_mutex);
  212. return -EINVAL;
  213. }
  214. /**
  215. * Get statistics information.
  216. *
  217. * \param inode device inode.
  218. * \param file_priv DRM file private.
  219. * \param cmd command.
  220. * \param arg user argument, pointing to a drm_stats structure.
  221. *
  222. * \return zero on success or a negative number on failure.
  223. */
  224. int drm_getstats(struct drm_device *dev, void *data,
  225. struct drm_file *file_priv)
  226. {
  227. struct drm_stats *stats = data;
  228. int i;
  229. memset(stats, 0, sizeof(*stats));
  230. for (i = 0; i < dev->counters; i++) {
  231. if (dev->types[i] == _DRM_STAT_LOCK)
  232. stats->data[i].value =
  233. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  234. else
  235. stats->data[i].value = atomic_read(&dev->counts[i]);
  236. stats->data[i].type = dev->types[i];
  237. }
  238. stats->count = dev->counters;
  239. return 0;
  240. }
  241. /**
  242. * Get device/driver capabilities
  243. */
  244. int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  245. {
  246. struct drm_get_cap *req = data;
  247. req->value = 0;
  248. switch (req->capability) {
  249. case DRM_CAP_DUMB_BUFFER:
  250. if (dev->driver->dumb_create)
  251. req->value = 1;
  252. break;
  253. case DRM_CAP_VBLANK_HIGH_CRTC:
  254. req->value = 1;
  255. break;
  256. case DRM_CAP_DUMB_PREFERRED_DEPTH:
  257. req->value = dev->mode_config.preferred_depth;
  258. break;
  259. case DRM_CAP_DUMB_PREFER_SHADOW:
  260. req->value = dev->mode_config.prefer_shadow;
  261. break;
  262. case DRM_CAP_PRIME:
  263. req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
  264. req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
  265. break;
  266. case DRM_CAP_TIMESTAMP_MONOTONIC:
  267. req->value = drm_timestamp_monotonic;
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. return 0;
  273. }
  274. /**
  275. * Setversion ioctl.
  276. *
  277. * \param inode device inode.
  278. * \param file_priv DRM file private.
  279. * \param cmd command.
  280. * \param arg user argument, pointing to a drm_lock structure.
  281. * \return zero on success or negative number on failure.
  282. *
  283. * Sets the requested interface version
  284. */
  285. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  286. {
  287. struct drm_set_version *sv = data;
  288. int if_version, retcode = 0;
  289. if (sv->drm_di_major != -1) {
  290. if (sv->drm_di_major != DRM_IF_MAJOR ||
  291. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  292. retcode = -EINVAL;
  293. goto done;
  294. }
  295. if_version = DRM_IF_VERSION(sv->drm_di_major,
  296. sv->drm_di_minor);
  297. dev->if_version = max(if_version, dev->if_version);
  298. if (sv->drm_di_minor >= 1) {
  299. /*
  300. * Version 1.1 includes tying of DRM to specific device
  301. * Version 1.4 has proper PCI domain support
  302. */
  303. retcode = drm_set_busid(dev, file_priv);
  304. if (retcode)
  305. goto done;
  306. }
  307. }
  308. if (sv->drm_dd_major != -1) {
  309. if (sv->drm_dd_major != dev->driver->major ||
  310. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  311. dev->driver->minor) {
  312. retcode = -EINVAL;
  313. goto done;
  314. }
  315. if (dev->driver->set_version)
  316. dev->driver->set_version(dev, sv);
  317. }
  318. done:
  319. sv->drm_di_major = DRM_IF_MAJOR;
  320. sv->drm_di_minor = DRM_IF_MINOR;
  321. sv->drm_dd_major = dev->driver->major;
  322. sv->drm_dd_minor = dev->driver->minor;
  323. return retcode;
  324. }
  325. /** No-op ioctl. */
  326. int drm_noop(struct drm_device *dev, void *data,
  327. struct drm_file *file_priv)
  328. {
  329. DRM_DEBUG("\n");
  330. return 0;
  331. }
  332. EXPORT_SYMBOL(drm_noop);