drm_ioctl.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "drmP.h"
  35. #include "drm_core.h"
  36. #include "linux/pci.h"
  37. /**
  38. * Get the bus id.
  39. *
  40. * \param inode device inode.
  41. * \param file_priv DRM file private.
  42. * \param cmd command.
  43. * \param arg user argument, pointing to a drm_unique structure.
  44. * \return zero on success or a negative number on failure.
  45. *
  46. * Copies the bus id from drm_device::unique into user space.
  47. */
  48. int drm_getunique(struct drm_device *dev, void *data,
  49. struct drm_file *file_priv)
  50. {
  51. struct drm_unique *u = data;
  52. struct drm_master *master = file_priv->master;
  53. if (u->unique_len >= master->unique_len) {
  54. if (copy_to_user(u->unique, master->unique, master->unique_len))
  55. return -EFAULT;
  56. }
  57. u->unique_len = master->unique_len;
  58. return 0;
  59. }
  60. static void
  61. drm_unset_busid(struct drm_device *dev,
  62. struct drm_master *master)
  63. {
  64. kfree(dev->devname);
  65. dev->devname = NULL;
  66. kfree(master->unique);
  67. master->unique = NULL;
  68. master->unique_len = 0;
  69. master->unique_size = 0;
  70. }
  71. /**
  72. * Set the bus id.
  73. *
  74. * \param inode device inode.
  75. * \param file_priv DRM file private.
  76. * \param cmd command.
  77. * \param arg user argument, pointing to a drm_unique structure.
  78. * \return zero on success or a negative number on failure.
  79. *
  80. * Copies the bus id from userspace into drm_device::unique, and verifies that
  81. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  82. * in interface version 1.1 and will return EBUSY when setversion has requested
  83. * version 1.1 or greater.
  84. */
  85. int drm_setunique(struct drm_device *dev, void *data,
  86. struct drm_file *file_priv)
  87. {
  88. struct drm_unique *u = data;
  89. struct drm_master *master = file_priv->master;
  90. int ret;
  91. if (master->unique_len || master->unique)
  92. return -EBUSY;
  93. if (!u->unique_len || u->unique_len > 1024)
  94. return -EINVAL;
  95. if (!dev->driver->bus->set_unique)
  96. return -EINVAL;
  97. ret = dev->driver->bus->set_unique(dev, master, u);
  98. if (ret)
  99. goto err;
  100. return 0;
  101. err:
  102. drm_unset_busid(dev, master);
  103. return ret;
  104. }
  105. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  106. {
  107. struct drm_master *master = file_priv->master;
  108. int ret;
  109. if (master->unique != NULL)
  110. drm_unset_busid(dev, master);
  111. ret = dev->driver->bus->set_busid(dev, master);
  112. if (ret)
  113. goto err;
  114. return 0;
  115. err:
  116. drm_unset_busid(dev, master);
  117. return ret;
  118. }
  119. /**
  120. * Get a mapping information.
  121. *
  122. * \param inode device inode.
  123. * \param file_priv DRM file private.
  124. * \param cmd command.
  125. * \param arg user argument, pointing to a drm_map structure.
  126. *
  127. * \return zero on success or a negative number on failure.
  128. *
  129. * Searches for the mapping with the specified offset and copies its information
  130. * into userspace
  131. */
  132. int drm_getmap(struct drm_device *dev, void *data,
  133. struct drm_file *file_priv)
  134. {
  135. struct drm_map *map = data;
  136. struct drm_map_list *r_list = NULL;
  137. struct list_head *list;
  138. int idx;
  139. int i;
  140. idx = map->offset;
  141. mutex_lock(&dev->struct_mutex);
  142. if (idx < 0) {
  143. mutex_unlock(&dev->struct_mutex);
  144. return -EINVAL;
  145. }
  146. i = 0;
  147. list_for_each(list, &dev->maplist) {
  148. if (i == idx) {
  149. r_list = list_entry(list, struct drm_map_list, head);
  150. break;
  151. }
  152. i++;
  153. }
  154. if (!r_list || !r_list->map) {
  155. mutex_unlock(&dev->struct_mutex);
  156. return -EINVAL;
  157. }
  158. map->offset = r_list->map->offset;
  159. map->size = r_list->map->size;
  160. map->type = r_list->map->type;
  161. map->flags = r_list->map->flags;
  162. map->handle = (void *)(unsigned long) r_list->user_token;
  163. map->mtrr = r_list->map->mtrr;
  164. mutex_unlock(&dev->struct_mutex);
  165. return 0;
  166. }
  167. /**
  168. * Get client information.
  169. *
  170. * \param inode device inode.
  171. * \param file_priv DRM file private.
  172. * \param cmd command.
  173. * \param arg user argument, pointing to a drm_client structure.
  174. *
  175. * \return zero on success or a negative number on failure.
  176. *
  177. * Searches for the client with the specified index and copies its information
  178. * into userspace
  179. */
  180. int drm_getclient(struct drm_device *dev, void *data,
  181. struct drm_file *file_priv)
  182. {
  183. struct drm_client *client = data;
  184. struct drm_file *pt;
  185. int idx;
  186. int i;
  187. idx = client->idx;
  188. mutex_lock(&dev->struct_mutex);
  189. i = 0;
  190. list_for_each_entry(pt, &dev->filelist, lhead) {
  191. if (i++ >= idx) {
  192. client->auth = pt->authenticated;
  193. client->pid = pt->pid;
  194. client->uid = pt->uid;
  195. client->magic = pt->magic;
  196. client->iocs = pt->ioctl_count;
  197. mutex_unlock(&dev->struct_mutex);
  198. return 0;
  199. }
  200. }
  201. mutex_unlock(&dev->struct_mutex);
  202. return -EINVAL;
  203. }
  204. /**
  205. * Get statistics information.
  206. *
  207. * \param inode device inode.
  208. * \param file_priv DRM file private.
  209. * \param cmd command.
  210. * \param arg user argument, pointing to a drm_stats structure.
  211. *
  212. * \return zero on success or a negative number on failure.
  213. */
  214. int drm_getstats(struct drm_device *dev, void *data,
  215. struct drm_file *file_priv)
  216. {
  217. struct drm_stats *stats = data;
  218. int i;
  219. memset(stats, 0, sizeof(*stats));
  220. mutex_lock(&dev->struct_mutex);
  221. for (i = 0; i < dev->counters; i++) {
  222. if (dev->types[i] == _DRM_STAT_LOCK)
  223. stats->data[i].value =
  224. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  225. else
  226. stats->data[i].value = atomic_read(&dev->counts[i]);
  227. stats->data[i].type = dev->types[i];
  228. }
  229. stats->count = dev->counters;
  230. mutex_unlock(&dev->struct_mutex);
  231. return 0;
  232. }
  233. /**
  234. * Get device/driver capabilities
  235. */
  236. int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  237. {
  238. struct drm_get_cap *req = data;
  239. req->value = 0;
  240. switch (req->capability) {
  241. case DRM_CAP_DUMB_BUFFER:
  242. if (dev->driver->dumb_create)
  243. req->value = 1;
  244. break;
  245. case DRM_CAP_VBLANK_HIGH_CRTC:
  246. req->value = 1;
  247. break;
  248. default:
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * Setversion ioctl.
  255. *
  256. * \param inode device inode.
  257. * \param file_priv DRM file private.
  258. * \param cmd command.
  259. * \param arg user argument, pointing to a drm_lock structure.
  260. * \return zero on success or negative number on failure.
  261. *
  262. * Sets the requested interface version
  263. */
  264. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  265. {
  266. struct drm_set_version *sv = data;
  267. int if_version, retcode = 0;
  268. if (sv->drm_di_major != -1) {
  269. if (sv->drm_di_major != DRM_IF_MAJOR ||
  270. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  271. retcode = -EINVAL;
  272. goto done;
  273. }
  274. if_version = DRM_IF_VERSION(sv->drm_di_major,
  275. sv->drm_di_minor);
  276. dev->if_version = max(if_version, dev->if_version);
  277. if (sv->drm_di_minor >= 1) {
  278. /*
  279. * Version 1.1 includes tying of DRM to specific device
  280. * Version 1.4 has proper PCI domain support
  281. */
  282. retcode = drm_set_busid(dev, file_priv);
  283. if (retcode)
  284. goto done;
  285. }
  286. }
  287. if (sv->drm_dd_major != -1) {
  288. if (sv->drm_dd_major != dev->driver->major ||
  289. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  290. dev->driver->minor) {
  291. retcode = -EINVAL;
  292. goto done;
  293. }
  294. if (dev->driver->set_version)
  295. dev->driver->set_version(dev, sv);
  296. }
  297. done:
  298. sv->drm_di_major = DRM_IF_MAJOR;
  299. sv->drm_di_minor = DRM_IF_MINOR;
  300. sv->drm_dd_major = dev->driver->major;
  301. sv->drm_dd_minor = dev->driver->minor;
  302. return retcode;
  303. }
  304. /** No-op ioctl. */
  305. int drm_noop(struct drm_device *dev, void *data,
  306. struct drm_file *file_priv)
  307. {
  308. DRM_DEBUG("\n");
  309. return 0;
  310. }