drm_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <drm/drmP.h>
  35. #include <drm/drm_core.h>
  36. #include <linux/pci.h>
  37. #include <linux/export.h>
  38. #ifdef CONFIG_X86
  39. #include <asm/mtrr.h>
  40. #endif
  41. /**
  42. * Get the bus id.
  43. *
  44. * \param inode device inode.
  45. * \param file_priv DRM file private.
  46. * \param cmd command.
  47. * \param arg user argument, pointing to a drm_unique structure.
  48. * \return zero on success or a negative number on failure.
  49. *
  50. * Copies the bus id from drm_device::unique into user space.
  51. */
  52. int drm_getunique(struct drm_device *dev, void *data,
  53. struct drm_file *file_priv)
  54. {
  55. struct drm_unique *u = data;
  56. struct drm_master *master = file_priv->master;
  57. if (u->unique_len >= master->unique_len) {
  58. if (copy_to_user(u->unique, master->unique, master->unique_len))
  59. return -EFAULT;
  60. }
  61. u->unique_len = master->unique_len;
  62. return 0;
  63. }
  64. static void
  65. drm_unset_busid(struct drm_device *dev,
  66. struct drm_master *master)
  67. {
  68. kfree(master->unique);
  69. master->unique = NULL;
  70. master->unique_len = 0;
  71. master->unique_size = 0;
  72. }
  73. /**
  74. * Set the bus id.
  75. *
  76. * \param inode device inode.
  77. * \param file_priv DRM file private.
  78. * \param cmd command.
  79. * \param arg user argument, pointing to a drm_unique structure.
  80. * \return zero on success or a negative number on failure.
  81. *
  82. * Copies the bus id from userspace into drm_device::unique, and verifies that
  83. * it matches the device this DRM is attached to (EINVAL otherwise). Deprecated
  84. * in interface version 1.1 and will return EBUSY when setversion has requested
  85. * version 1.1 or greater. Also note that KMS is all version 1.1 and later and
  86. * UMS was only ever supported on pci devices.
  87. */
  88. int drm_setunique(struct drm_device *dev, void *data,
  89. struct drm_file *file_priv)
  90. {
  91. struct drm_unique *u = data;
  92. struct drm_master *master = file_priv->master;
  93. int ret;
  94. if (master->unique_len || master->unique)
  95. return -EBUSY;
  96. if (!u->unique_len || u->unique_len > 1024)
  97. return -EINVAL;
  98. if (drm_core_check_feature(dev, DRIVER_MODESET))
  99. return 0;
  100. if (WARN_ON(!dev->pdev))
  101. return -EINVAL;
  102. ret = drm_pci_set_unique(dev, master, u);
  103. if (ret)
  104. goto err;
  105. return 0;
  106. err:
  107. drm_unset_busid(dev, master);
  108. return ret;
  109. }
  110. static int drm_set_busid(struct drm_device *dev, struct drm_file *file_priv)
  111. {
  112. struct drm_master *master = file_priv->master;
  113. int ret;
  114. if (master->unique != NULL)
  115. drm_unset_busid(dev, master);
  116. if (dev->driver->bus && dev->driver->bus->set_busid) {
  117. ret = dev->driver->bus->set_busid(dev, master);
  118. if (ret) {
  119. drm_unset_busid(dev, master);
  120. return ret;
  121. }
  122. } else {
  123. if (WARN(dev->unique == NULL,
  124. "No drm_bus.set_busid() implementation provided by "
  125. "%ps. Use drm_dev_set_unique() to set the unique "
  126. "name explicitly.", dev->driver))
  127. return -EINVAL;
  128. master->unique = kstrdup(dev->unique, GFP_KERNEL);
  129. if (master->unique)
  130. master->unique_len = strlen(dev->unique);
  131. }
  132. return 0;
  133. }
  134. /**
  135. * Get a mapping information.
  136. *
  137. * \param inode device inode.
  138. * \param file_priv DRM file private.
  139. * \param cmd command.
  140. * \param arg user argument, pointing to a drm_map structure.
  141. *
  142. * \return zero on success or a negative number on failure.
  143. *
  144. * Searches for the mapping with the specified offset and copies its information
  145. * into userspace
  146. */
  147. int drm_getmap(struct drm_device *dev, void *data,
  148. struct drm_file *file_priv)
  149. {
  150. struct drm_map *map = data;
  151. struct drm_map_list *r_list = NULL;
  152. struct list_head *list;
  153. int idx;
  154. int i;
  155. idx = map->offset;
  156. if (idx < 0)
  157. return -EINVAL;
  158. i = 0;
  159. mutex_lock(&dev->struct_mutex);
  160. list_for_each(list, &dev->maplist) {
  161. if (i == idx) {
  162. r_list = list_entry(list, struct drm_map_list, head);
  163. break;
  164. }
  165. i++;
  166. }
  167. if (!r_list || !r_list->map) {
  168. mutex_unlock(&dev->struct_mutex);
  169. return -EINVAL;
  170. }
  171. map->offset = r_list->map->offset;
  172. map->size = r_list->map->size;
  173. map->type = r_list->map->type;
  174. map->flags = r_list->map->flags;
  175. map->handle = (void *)(unsigned long) r_list->user_token;
  176. #ifdef CONFIG_X86
  177. /*
  178. * There appears to be exactly one user of the mtrr index: dritest.
  179. * It's easy enough to keep it working on non-PAT systems.
  180. */
  181. map->mtrr = phys_wc_to_mtrr_index(r_list->map->mtrr);
  182. #else
  183. map->mtrr = -1;
  184. #endif
  185. mutex_unlock(&dev->struct_mutex);
  186. return 0;
  187. }
  188. /**
  189. * Get client information.
  190. *
  191. * \param inode device inode.
  192. * \param file_priv DRM file private.
  193. * \param cmd command.
  194. * \param arg user argument, pointing to a drm_client structure.
  195. *
  196. * \return zero on success or a negative number on failure.
  197. *
  198. * Searches for the client with the specified index and copies its information
  199. * into userspace
  200. */
  201. int drm_getclient(struct drm_device *dev, void *data,
  202. struct drm_file *file_priv)
  203. {
  204. struct drm_client *client = data;
  205. /*
  206. * Hollowed-out getclient ioctl to keep some dead old drm tests/tools
  207. * not breaking completely. Userspace tools stop enumerating one they
  208. * get -EINVAL, hence this is the return value we need to hand back for
  209. * no clients tracked.
  210. *
  211. * Unfortunately some clients (*cough* libva *cough*) use this in a fun
  212. * attempt to figure out whether they're authenticated or not. Since
  213. * that's the only thing they care about, give it to the directly
  214. * instead of walking one giant list.
  215. */
  216. if (client->idx == 0) {
  217. client->auth = file_priv->authenticated;
  218. client->pid = pid_vnr(file_priv->pid);
  219. client->uid = from_kuid_munged(current_user_ns(),
  220. file_priv->uid);
  221. client->magic = 0;
  222. client->iocs = 0;
  223. return 0;
  224. } else {
  225. return -EINVAL;
  226. }
  227. }
  228. /**
  229. * Get statistics information.
  230. *
  231. * \param inode device inode.
  232. * \param file_priv DRM file private.
  233. * \param cmd command.
  234. * \param arg user argument, pointing to a drm_stats structure.
  235. *
  236. * \return zero on success or a negative number on failure.
  237. */
  238. int drm_getstats(struct drm_device *dev, void *data,
  239. struct drm_file *file_priv)
  240. {
  241. struct drm_stats *stats = data;
  242. /* Clear stats to prevent userspace from eating its stack garbage. */
  243. memset(stats, 0, sizeof(*stats));
  244. return 0;
  245. }
  246. /**
  247. * Get device/driver capabilities
  248. */
  249. int drm_getcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  250. {
  251. struct drm_get_cap *req = data;
  252. req->value = 0;
  253. switch (req->capability) {
  254. case DRM_CAP_DUMB_BUFFER:
  255. if (dev->driver->dumb_create)
  256. req->value = 1;
  257. break;
  258. case DRM_CAP_VBLANK_HIGH_CRTC:
  259. req->value = 1;
  260. break;
  261. case DRM_CAP_DUMB_PREFERRED_DEPTH:
  262. req->value = dev->mode_config.preferred_depth;
  263. break;
  264. case DRM_CAP_DUMB_PREFER_SHADOW:
  265. req->value = dev->mode_config.prefer_shadow;
  266. break;
  267. case DRM_CAP_PRIME:
  268. req->value |= dev->driver->prime_fd_to_handle ? DRM_PRIME_CAP_IMPORT : 0;
  269. req->value |= dev->driver->prime_handle_to_fd ? DRM_PRIME_CAP_EXPORT : 0;
  270. break;
  271. case DRM_CAP_TIMESTAMP_MONOTONIC:
  272. req->value = drm_timestamp_monotonic;
  273. break;
  274. case DRM_CAP_ASYNC_PAGE_FLIP:
  275. req->value = dev->mode_config.async_page_flip;
  276. break;
  277. case DRM_CAP_CURSOR_WIDTH:
  278. if (dev->mode_config.cursor_width)
  279. req->value = dev->mode_config.cursor_width;
  280. else
  281. req->value = 64;
  282. break;
  283. case DRM_CAP_CURSOR_HEIGHT:
  284. if (dev->mode_config.cursor_height)
  285. req->value = dev->mode_config.cursor_height;
  286. else
  287. req->value = 64;
  288. break;
  289. default:
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. /**
  295. * Set device/driver capabilities
  296. */
  297. int
  298. drm_setclientcap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  299. {
  300. struct drm_set_client_cap *req = data;
  301. switch (req->capability) {
  302. case DRM_CLIENT_CAP_STEREO_3D:
  303. if (req->value > 1)
  304. return -EINVAL;
  305. file_priv->stereo_allowed = req->value;
  306. break;
  307. case DRM_CLIENT_CAP_UNIVERSAL_PLANES:
  308. if (!drm_universal_planes)
  309. return -EINVAL;
  310. if (req->value > 1)
  311. return -EINVAL;
  312. file_priv->universal_planes = req->value;
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. return 0;
  318. }
  319. /**
  320. * Setversion ioctl.
  321. *
  322. * \param inode device inode.
  323. * \param file_priv DRM file private.
  324. * \param cmd command.
  325. * \param arg user argument, pointing to a drm_lock structure.
  326. * \return zero on success or negative number on failure.
  327. *
  328. * Sets the requested interface version
  329. */
  330. int drm_setversion(struct drm_device *dev, void *data, struct drm_file *file_priv)
  331. {
  332. struct drm_set_version *sv = data;
  333. int if_version, retcode = 0;
  334. if (sv->drm_di_major != -1) {
  335. if (sv->drm_di_major != DRM_IF_MAJOR ||
  336. sv->drm_di_minor < 0 || sv->drm_di_minor > DRM_IF_MINOR) {
  337. retcode = -EINVAL;
  338. goto done;
  339. }
  340. if_version = DRM_IF_VERSION(sv->drm_di_major,
  341. sv->drm_di_minor);
  342. dev->if_version = max(if_version, dev->if_version);
  343. if (sv->drm_di_minor >= 1) {
  344. /*
  345. * Version 1.1 includes tying of DRM to specific device
  346. * Version 1.4 has proper PCI domain support
  347. */
  348. retcode = drm_set_busid(dev, file_priv);
  349. if (retcode)
  350. goto done;
  351. }
  352. }
  353. if (sv->drm_dd_major != -1) {
  354. if (sv->drm_dd_major != dev->driver->major ||
  355. sv->drm_dd_minor < 0 || sv->drm_dd_minor >
  356. dev->driver->minor) {
  357. retcode = -EINVAL;
  358. goto done;
  359. }
  360. }
  361. done:
  362. sv->drm_di_major = DRM_IF_MAJOR;
  363. sv->drm_di_minor = DRM_IF_MINOR;
  364. sv->drm_dd_major = dev->driver->major;
  365. sv->drm_dd_minor = dev->driver->minor;
  366. return retcode;
  367. }
  368. /** No-op ioctl. */
  369. int drm_noop(struct drm_device *dev, void *data,
  370. struct drm_file *file_priv)
  371. {
  372. DRM_DEBUG("\n");
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(drm_noop);