drm_ioctl.c 9.8 KB

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