drm_ioctl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 = kmalloc(master->unique_size, GFP_KERNEL);
  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 = kmalloc(strlen(dev->driver->pci_driver.name) +
  93. strlen(master->unique) + 2, GFP_KERNEL);
  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_size = master->unique_len;
  121. master->unique = kmalloc(master->unique_size, GFP_KERNEL);
  122. if (master->unique == NULL)
  123. return -ENOMEM;
  124. len = snprintf(master->unique, master->unique_len, "pci:%04x:%02x:%02x.%d",
  125. drm_get_pci_domain(dev),
  126. dev->pdev->bus->number,
  127. PCI_SLOT(dev->pdev->devfn),
  128. PCI_FUNC(dev->pdev->devfn));
  129. if (len >= master->unique_len)
  130. DRM_ERROR("buffer overflow");
  131. else
  132. master->unique_len = len;
  133. dev->devname = kmalloc(strlen(dev->driver->pci_driver.name) +
  134. master->unique_len + 2, GFP_KERNEL);
  135. if (dev->devname == NULL)
  136. return -ENOMEM;
  137. sprintf(dev->devname, "%s@%s", dev->driver->pci_driver.name,
  138. master->unique);
  139. return 0;
  140. }
  141. /**
  142. * Get a mapping information.
  143. *
  144. * \param inode device inode.
  145. * \param file_priv DRM file private.
  146. * \param cmd command.
  147. * \param arg user argument, pointing to a drm_map structure.
  148. *
  149. * \return zero on success or a negative number on failure.
  150. *
  151. * Searches for the mapping with the specified offset and copies its information
  152. * into userspace
  153. */
  154. int drm_getmap(struct drm_device *dev, void *data,
  155. struct drm_file *file_priv)
  156. {
  157. struct drm_map *map = data;
  158. struct drm_map_list *r_list = NULL;
  159. struct list_head *list;
  160. int idx;
  161. int i;
  162. idx = map->offset;
  163. mutex_lock(&dev->struct_mutex);
  164. if (idx < 0) {
  165. mutex_unlock(&dev->struct_mutex);
  166. return -EINVAL;
  167. }
  168. i = 0;
  169. list_for_each(list, &dev->maplist) {
  170. if (i == idx) {
  171. r_list = list_entry(list, struct drm_map_list, head);
  172. break;
  173. }
  174. i++;
  175. }
  176. if (!r_list || !r_list->map) {
  177. mutex_unlock(&dev->struct_mutex);
  178. return -EINVAL;
  179. }
  180. map->offset = r_list->map->offset;
  181. map->size = r_list->map->size;
  182. map->type = r_list->map->type;
  183. map->flags = r_list->map->flags;
  184. map->handle = (void *)(unsigned long) r_list->user_token;
  185. map->mtrr = r_list->map->mtrr;
  186. mutex_unlock(&dev->struct_mutex);
  187. return 0;
  188. }
  189. /**
  190. * Get client information.
  191. *
  192. * \param inode device inode.
  193. * \param file_priv DRM file private.
  194. * \param cmd command.
  195. * \param arg user argument, pointing to a drm_client structure.
  196. *
  197. * \return zero on success or a negative number on failure.
  198. *
  199. * Searches for the client with the specified index and copies its information
  200. * into userspace
  201. */
  202. int drm_getclient(struct drm_device *dev, void *data,
  203. struct drm_file *file_priv)
  204. {
  205. struct drm_client *client = data;
  206. struct drm_file *pt;
  207. int idx;
  208. int i;
  209. idx = client->idx;
  210. mutex_lock(&dev->struct_mutex);
  211. i = 0;
  212. list_for_each_entry(pt, &dev->filelist, lhead) {
  213. if (i++ >= idx) {
  214. client->auth = pt->authenticated;
  215. client->pid = pt->pid;
  216. client->uid = pt->uid;
  217. client->magic = pt->magic;
  218. client->iocs = pt->ioctl_count;
  219. mutex_unlock(&dev->struct_mutex);
  220. return 0;
  221. }
  222. }
  223. mutex_unlock(&dev->struct_mutex);
  224. return -EINVAL;
  225. }
  226. /**
  227. * Get statistics information.
  228. *
  229. * \param inode device inode.
  230. * \param file_priv DRM file private.
  231. * \param cmd command.
  232. * \param arg user argument, pointing to a drm_stats structure.
  233. *
  234. * \return zero on success or a negative number on failure.
  235. */
  236. int drm_getstats(struct drm_device *dev, void *data,
  237. struct drm_file *file_priv)
  238. {
  239. struct drm_stats *stats = data;
  240. int i;
  241. memset(stats, 0, sizeof(*stats));
  242. mutex_lock(&dev->struct_mutex);
  243. for (i = 0; i < dev->counters; i++) {
  244. if (dev->types[i] == _DRM_STAT_LOCK)
  245. stats->data[i].value =
  246. (file_priv->master->lock.hw_lock ? file_priv->master->lock.hw_lock->lock : 0);
  247. else
  248. stats->data[i].value = atomic_read(&dev->counts[i]);
  249. stats->data[i].type = dev->types[i];
  250. }
  251. stats->count = dev->counters;
  252. mutex_unlock(&dev->struct_mutex);
  253. return 0;
  254. }
  255. /**
  256. * Setversion ioctl.
  257. *
  258. * \param inode device inode.
  259. * \param file_priv DRM file private.
  260. * \param cmd command.
  261. * \param arg user argument, pointing to a drm_lock structure.
  262. * \return zero on success or negative number on failure.
  263. *
  264. * Sets the requested interface version
  265. */
  266. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  267. {
  268. struct drm_set_version *sv = data;
  269. int if_version, retcode = 0;
  270. if (sv->drm_di_major != -1) {
  271. if (sv->drm_di_major != DRM_IF_MAJOR ||
  272. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  273. retcode = -EINVAL;
  274. goto done;
  275. }
  276. if_version = DRM_IF_VERSION(sv->drm_di_major,
  277. sv->drm_di_minor);
  278. dev->if_version = max(if_version, dev->if_version);
  279. if (sv->drm_di_minor >= 1) {
  280. /*
  281. * Version 1.1 includes tying of DRM to specific device
  282. */
  283. drm_set_busid(dev, file_priv);
  284. }
  285. }
  286. if (sv->drm_dd_major != -1) {
  287. if (sv->drm_dd_major != dev->driver->major ||
  288. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  289. dev->driver->minor) {
  290. retcode = -EINVAL;
  291. goto done;
  292. }
  293. if (dev->driver->set_version)
  294. dev->driver->set_version(dev, sv);
  295. }
  296. done:
  297. sv->drm_di_major = DRM_IF_MAJOR;
  298. sv->drm_di_minor = DRM_IF_MINOR;
  299. sv->drm_dd_major = dev->driver->major;
  300. sv->drm_dd_minor = dev->driver->minor;
  301. return retcode;
  302. }
  303. /** No-op ioctl. */
  304. int drm_noop(struct drm_device *dev, void *data,
  305. struct drm_file *file_priv)
  306. {
  307. DRM_DEBUG("\n");
  308. return 0;
  309. }