drm_ioctl.c 9.8 KB

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