drm_ioctl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 != dev->pci_domain) ||
  116. (bus != dev->pci_bus) ||
  117. (slot != dev->pci_slot) || (func != dev->pci_func))
  118. return -EINVAL;
  119. return 0;
  120. }
  121. static int drm_set_busid(drm_device_t * dev)
  122. {
  123. int len;
  124. if (dev->unique != NULL)
  125. return EBUSY;
  126. dev->unique_len = 40;
  127. dev->unique = drm_alloc(dev->unique_len + 1, DRM_MEM_DRIVER);
  128. if (dev->unique == NULL)
  129. return ENOMEM;
  130. len = snprintf(dev->unique, dev->unique_len, "pci:%04x:%02x:%02x.%d",
  131. dev->pci_domain, dev->pci_bus, dev->pci_slot, dev->pci_func);
  132. if (len > dev->unique_len)
  133. DRM_ERROR("Unique buffer overflowed\n");
  134. dev->devname =
  135. drm_alloc(strlen(dev->driver->pci_driver.name) + dev->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. dev->unique);
  141. return 0;
  142. }
  143. /**
  144. * Get a mapping information.
  145. *
  146. * \param inode device inode.
  147. * \param filp file pointer.
  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 inode *inode, struct file *filp,
  157. unsigned int cmd, unsigned long arg)
  158. {
  159. drm_file_t *priv = filp->private_data;
  160. drm_device_t *dev = priv->head->dev;
  161. drm_map_t __user *argp = (void __user *)arg;
  162. drm_map_t map;
  163. drm_map_list_t *r_list = NULL;
  164. struct list_head *list;
  165. int idx;
  166. int i;
  167. if (copy_from_user(&map, argp, sizeof(map)))
  168. return -EFAULT;
  169. idx = map.offset;
  170. mutex_lock(&dev->struct_mutex);
  171. if (idx < 0) {
  172. mutex_unlock(&dev->struct_mutex);
  173. return -EINVAL;
  174. }
  175. i = 0;
  176. list_for_each(list, &dev->maplist->head) {
  177. if (i == idx) {
  178. r_list = list_entry(list, drm_map_list_t, head);
  179. break;
  180. }
  181. i++;
  182. }
  183. if (!r_list || !r_list->map) {
  184. mutex_unlock(&dev->struct_mutex);
  185. return -EINVAL;
  186. }
  187. map.offset = r_list->map->offset;
  188. map.size = r_list->map->size;
  189. map.type = r_list->map->type;
  190. map.flags = r_list->map->flags;
  191. map.handle = (void *)(unsigned long)r_list->user_token;
  192. map.mtrr = r_list->map->mtrr;
  193. mutex_unlock(&dev->struct_mutex);
  194. if (copy_to_user(argp, &map, sizeof(map)))
  195. return -EFAULT;
  196. return 0;
  197. }
  198. /**
  199. * Get client information.
  200. *
  201. * \param inode device inode.
  202. * \param filp file pointer.
  203. * \param cmd command.
  204. * \param arg user argument, pointing to a drm_client structure.
  205. *
  206. * \return zero on success or a negative number on failure.
  207. *
  208. * Searches for the client with the specified index and copies its information
  209. * into userspace
  210. */
  211. int drm_getclient(struct inode *inode, struct file *filp,
  212. unsigned int cmd, unsigned long arg)
  213. {
  214. drm_file_t *priv = filp->private_data;
  215. drm_device_t *dev = priv->head->dev;
  216. drm_client_t __user *argp = (drm_client_t __user *)arg;
  217. drm_client_t client;
  218. drm_file_t *pt;
  219. int idx;
  220. int i;
  221. if (copy_from_user(&client, argp, sizeof(client)))
  222. return -EFAULT;
  223. idx = client.idx;
  224. mutex_lock(&dev->struct_mutex);
  225. for (i = 0, pt = dev->file_first; i < idx && pt; i++, pt = pt->next) ;
  226. if (!pt) {
  227. mutex_unlock(&dev->struct_mutex);
  228. return -EINVAL;
  229. }
  230. client.auth = pt->authenticated;
  231. client.pid = pt->pid;
  232. client.uid = pt->uid;
  233. client.magic = pt->magic;
  234. client.iocs = pt->ioctl_count;
  235. mutex_unlock(&dev->struct_mutex);
  236. if (copy_to_user(argp, &client, sizeof(client)))
  237. return -EFAULT;
  238. return 0;
  239. }
  240. /**
  241. * Get statistics information.
  242. *
  243. * \param inode device inode.
  244. * \param filp file pointer.
  245. * \param cmd command.
  246. * \param arg user argument, pointing to a drm_stats structure.
  247. *
  248. * \return zero on success or a negative number on failure.
  249. */
  250. int drm_getstats(struct inode *inode, struct file *filp,
  251. unsigned int cmd, unsigned long arg)
  252. {
  253. drm_file_t *priv = filp->private_data;
  254. drm_device_t *dev = priv->head->dev;
  255. drm_stats_t stats;
  256. int i;
  257. memset(&stats, 0, sizeof(stats));
  258. mutex_lock(&dev->struct_mutex);
  259. for (i = 0; i < dev->counters; i++) {
  260. if (dev->types[i] == _DRM_STAT_LOCK)
  261. stats.data[i].value
  262. = (dev->lock.hw_lock ? dev->lock.hw_lock->lock : 0);
  263. else
  264. stats.data[i].value = atomic_read(&dev->counts[i]);
  265. stats.data[i].type = dev->types[i];
  266. }
  267. stats.count = dev->counters;
  268. mutex_unlock(&dev->struct_mutex);
  269. if (copy_to_user((drm_stats_t __user *) arg, &stats, sizeof(stats)))
  270. return -EFAULT;
  271. return 0;
  272. }
  273. /**
  274. * Setversion ioctl.
  275. *
  276. * \param inode device inode.
  277. * \param filp file pointer.
  278. * \param cmd command.
  279. * \param arg user argument, pointing to a drm_lock structure.
  280. * \return zero on success or negative number on failure.
  281. *
  282. * Sets the requested interface version
  283. */
  284. int drm_setversion(DRM_IOCTL_ARGS)
  285. {
  286. DRM_DEVICE;
  287. drm_set_version_t sv;
  288. drm_set_version_t retv;
  289. int if_version;
  290. drm_set_version_t __user *argp = (void __user *)data;
  291. DRM_COPY_FROM_USER_IOCTL(sv, argp, sizeof(sv));
  292. retv.drm_di_major = DRM_IF_MAJOR;
  293. retv.drm_di_minor = DRM_IF_MINOR;
  294. retv.drm_dd_major = dev->driver->major;
  295. retv.drm_dd_minor = dev->driver->minor;
  296. DRM_COPY_TO_USER_IOCTL(argp, retv, sizeof(sv));
  297. if (sv.drm_di_major != -1) {
  298. if (sv.drm_di_major != DRM_IF_MAJOR ||
  299. sv.drm_di_minor < 0 || sv.drm_di_minor > DRM_IF_MINOR)
  300. return EINVAL;
  301. if_version = DRM_IF_VERSION(sv.drm_di_major, sv.drm_di_minor);
  302. dev->if_version = DRM_MAX(if_version, dev->if_version);
  303. if (sv.drm_di_minor >= 1) {
  304. /*
  305. * Version 1.1 includes tying of DRM to specific device
  306. */
  307. drm_set_busid(dev);
  308. }
  309. }
  310. if (sv.drm_dd_major != -1) {
  311. if (sv.drm_dd_major != dev->driver->major ||
  312. sv.drm_dd_minor < 0
  313. || sv.drm_dd_minor > dev->driver->minor)
  314. return EINVAL;
  315. if (dev->driver->set_version)
  316. dev->driver->set_version(dev, &sv);
  317. }
  318. return 0;
  319. }
  320. /** No-op ioctl. */
  321. int drm_noop(struct inode *inode, struct file *filp, unsigned int cmd,
  322. unsigned long arg)
  323. {
  324. DRM_DEBUG("\n");
  325. return 0;
  326. }