drm_ioctl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = drm_alloc(u->unique_len + 1, DRM_MEM_DRIVER);
  86. if (!master->unique)
  87. return -ENOMEM;
  88. if (copy_from_user(master->unique, u->unique, master->unique_len))
  89. return -EFAULT;
  90. master->unique[master->unique_len] = '\0';
  91. dev->devname =
  92. drm_alloc(strlen(dev->driver->pci_driver.name) +
  93. strlen(master->unique) + 2, DRM_MEM_DRIVER);
  94. if (!dev->devname)
  95. return -ENOMEM;
  96. sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
  97. master->unique);
  98. /* Return error if the busid submitted doesn't match the device's actual
  99. * busid.
  100. */
  101. ret = sscanf(master->unique, "PCI:%d:%d:%d", &bus, &slot, &func);
  102. if (ret != 3)
  103. return -EINVAL;
  104. domain = bus >> 8;
  105. bus &= 0xff;
  106. if ((domain != drm_get_pci_domain(dev)) ||
  107. (bus != dev->pdev->bus->number) ||
  108. (slot != PCI_SLOT(dev->pdev->devfn)) ||
  109. (func != PCI_FUNC(dev->pdev->devfn)))
  110. return -EINVAL;
  111. return 0;
  112. }
  113. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  114. {
  115. struct drm_master *master = file_priv->master;
  116. int len;
  117. if (master->unique != NULL)
  118. return -EBUSY;
  119. master->unique_len = 40;
  120. master->unique = drm_alloc(master->unique_len + 1, DRM_MEM_DRIVER);
  121. if (master->unique == NULL)
  122. return -ENOMEM;
  123. len = snprintf(master->unique, master->unique_len, "pci:%04x:%02x:%02x.%d",
  124. drm_get_pci_domain(dev),
  125. dev->pdev->bus->number,
  126. PCI_SLOT(dev->pdev->devfn),
  127. PCI_FUNC(dev->pdev->devfn));
  128. if (len > master->unique_len)
  129. DRM_ERROR("buffer overflow");
  130. dev->devname =
  131. drm_alloc(strlen(dev->driver->pci_driver.name) + master->unique_len +
  132. 2, DRM_MEM_DRIVER);
  133. if (dev->devname == NULL)
  134. return -ENOMEM;
  135. sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
  136. master->unique);
  137. return 0;
  138. }
  139. /**
  140. * Get a mapping information.
  141. *
  142. * \param inode device inode.
  143. * \param file_priv DRM file private.
  144. * \param cmd command.
  145. * \param arg user argument, pointing to a drm_map structure.
  146. *
  147. * \return zero on success or a negative number on failure.
  148. *
  149. * Searches for the mapping with the specified offset and copies its information
  150. * into userspace
  151. */
  152. int drm_getmap(struct drm_device *dev, void *data,
  153. struct drm_file *file_priv)
  154. {
  155. struct drm_map *map = data;
  156. struct drm_map_list *r_list = NULL;
  157. struct list_head *list;
  158. int idx;
  159. int i;
  160. idx = map->offset;
  161. mutex_lock(&dev->struct_mutex);
  162. if (idx < 0) {
  163. mutex_unlock(&dev->struct_mutex);
  164. return -EINVAL;
  165. }
  166. i = 0;
  167. list_for_each(list, &dev->maplist) {
  168. if (i == idx) {
  169. r_list = list_entry(list, struct drm_map_list, head);
  170. break;
  171. }
  172. i++;
  173. }
  174. if (!r_list || !r_list->map) {
  175. mutex_unlock(&dev->struct_mutex);
  176. return -EINVAL;
  177. }
  178. map->offset = r_list->map->offset;
  179. map->size = r_list->map->size;
  180. map->type = r_list->map->type;
  181. map->flags = r_list->map->flags;
  182. map->handle = (void *)(unsigned long) r_list->user_token;
  183. map->mtrr = r_list->map->mtrr;
  184. mutex_unlock(&dev->struct_mutex);
  185. return 0;
  186. }
  187. /**
  188. * Get client information.
  189. *
  190. * \param inode device inode.
  191. * \param file_priv DRM file private.
  192. * \param cmd command.
  193. * \param arg user argument, pointing to a drm_client structure.
  194. *
  195. * \return zero on success or a negative number on failure.
  196. *
  197. * Searches for the client with the specified index and copies its information
  198. * into userspace
  199. */
  200. int drm_getclient(struct drm_device *dev, void *data,
  201. struct drm_file *file_priv)
  202. {
  203. struct drm_client *client = data;
  204. struct drm_file *pt;
  205. int idx;
  206. int i;
  207. idx = client->idx;
  208. mutex_lock(&dev->struct_mutex);
  209. i = 0;
  210. list_for_each_entry(pt, &dev->filelist, lhead) {
  211. if (i++ >= idx) {
  212. client->auth = pt->authenticated;
  213. client->pid = pt->pid;
  214. client->uid = pt->uid;
  215. client->magic = pt->magic;
  216. client->iocs = pt->ioctl_count;
  217. mutex_unlock(&dev->struct_mutex);
  218. return 0;
  219. }
  220. }
  221. mutex_unlock(&dev->struct_mutex);
  222. return -EINVAL;
  223. }
  224. /**
  225. * Get statistics information.
  226. *
  227. * \param inode device inode.
  228. * \param file_priv DRM file private.
  229. * \param cmd command.
  230. * \param arg user argument, pointing to a drm_stats structure.
  231. *
  232. * \return zero on success or a negative number on failure.
  233. */
  234. int drm_getstats(struct drm_device *dev, void *data,
  235. struct drm_file *file_priv)
  236. {
  237. struct drm_stats *stats = data;
  238. int i;
  239. memset(stats, 0, sizeof(*stats));
  240. mutex_lock(&dev->struct_mutex);
  241. for (i = 0; i < dev->counters; i++) {
  242. if (dev->types[i] == _DRM_STAT_LOCK)
  243. stats->data[i].value =
  244. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  245. else
  246. stats->data[i].value = atomic_read(&dev->counts[i]);
  247. stats->data[i].type = dev->types[i];
  248. }
  249. stats->count = dev->counters;
  250. mutex_unlock(&dev->struct_mutex);
  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. */
  281. drm_set_busid(dev, file_priv);
  282. }
  283. }
  284. if (sv->drm_dd_major != -1) {
  285. if (sv->drm_dd_major != dev->driver->major ||
  286. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  287. dev->driver->minor) {
  288. retcode = -EINVAL;
  289. goto done;
  290. }
  291. if (dev->driver->set_version)
  292. dev->driver->set_version(dev, sv);
  293. }
  294. done:
  295. sv->drm_di_major = DRM_IF_MAJOR;
  296. sv->drm_di_minor = DRM_IF_MINOR;
  297. sv->drm_dd_major = dev->driver->major;
  298. sv->drm_dd_minor = dev->driver->minor;
  299. return retcode;
  300. }
  301. /** No-op ioctl. */
  302. int drm_noop(struct drm_device *dev, void *data,
  303. struct drm_file *file_priv)
  304. {
  305. DRM_DEBUG("\n");
  306. return 0;
  307. }