drm_ioctl.c 9.2 KB

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