drm_ioctl.c 10 KB

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