drm_ioctl.c 9.6 KB

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