drm_ioctl.c 9.9 KB

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