drm_ioctl.c 8.8 KB

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