drm_fops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**
  2. * \file drm_fops.c
  3. * File operations for DRM
  4. *
  5. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  6. * \author Daryll Strauss <daryll@valinux.com>
  7. * \author Gareth Hughes <gareth@valinux.com>
  8. */
  9. /*
  10. * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
  11. *
  12. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  13. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  14. * All Rights Reserved.
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files (the "Software"),
  18. * to deal in the Software without restriction, including without limitation
  19. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. * and/or sell copies of the Software, and to permit persons to whom the
  21. * Software is furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice (including the next
  24. * paragraph) shall be included in all copies or substantial portions of the
  25. * Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33. * OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #include <drm/drmP.h>
  36. #include <linux/poll.h>
  37. #include <linux/slab.h>
  38. #include <linux/module.h>
  39. /* from BKL pushdown: note that nothing else serializes idr_find() */
  40. DEFINE_MUTEX(drm_global_mutex);
  41. EXPORT_SYMBOL(drm_global_mutex);
  42. static int drm_open_helper(struct inode *inode, struct file *filp,
  43. struct drm_device * dev);
  44. static int drm_setup(struct drm_device * dev)
  45. {
  46. int ret;
  47. if (dev->driver->firstopen &&
  48. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  49. ret = dev->driver->firstopen(dev);
  50. if (ret != 0)
  51. return ret;
  52. }
  53. ret = drm_legacy_dma_setup(dev);
  54. if (ret < 0)
  55. return ret;
  56. DRM_DEBUG("\n");
  57. return 0;
  58. }
  59. /**
  60. * Open file.
  61. *
  62. * \param inode device inode
  63. * \param filp file pointer.
  64. * \return zero on success or a negative number on failure.
  65. *
  66. * Searches the DRM device with the same minor number, calls open_helper(), and
  67. * increments the device open count. If the open count was previous at zero,
  68. * i.e., it's the first that the device is open, then calls setup().
  69. */
  70. int drm_open(struct inode *inode, struct file *filp)
  71. {
  72. struct drm_device *dev = NULL;
  73. int minor_id = iminor(inode);
  74. struct drm_minor *minor;
  75. int retcode = 0;
  76. int need_setup = 0;
  77. minor = idr_find(&drm_minors_idr, minor_id);
  78. if (!minor)
  79. return -ENODEV;
  80. if (!(dev = minor->dev))
  81. return -ENODEV;
  82. if (drm_device_is_unplugged(dev))
  83. return -ENODEV;
  84. if (!dev->open_count++)
  85. need_setup = 1;
  86. /* share address_space across all char-devs of a single device */
  87. filp->f_mapping = dev->anon_inode->i_mapping;
  88. retcode = drm_open_helper(inode, filp, dev);
  89. if (retcode)
  90. goto err_undo;
  91. if (need_setup) {
  92. retcode = drm_setup(dev);
  93. if (retcode)
  94. goto err_undo;
  95. }
  96. return 0;
  97. err_undo:
  98. dev->open_count--;
  99. return retcode;
  100. }
  101. EXPORT_SYMBOL(drm_open);
  102. /**
  103. * File \c open operation.
  104. *
  105. * \param inode device inode.
  106. * \param filp file pointer.
  107. *
  108. * Puts the dev->fops corresponding to the device minor number into
  109. * \p filp, call the \c open method, and restore the file operations.
  110. */
  111. int drm_stub_open(struct inode *inode, struct file *filp)
  112. {
  113. struct drm_device *dev = NULL;
  114. struct drm_minor *minor;
  115. int minor_id = iminor(inode);
  116. int err = -ENODEV;
  117. const struct file_operations *new_fops;
  118. DRM_DEBUG("\n");
  119. mutex_lock(&drm_global_mutex);
  120. minor = idr_find(&drm_minors_idr, minor_id);
  121. if (!minor)
  122. goto out;
  123. if (!(dev = minor->dev))
  124. goto out;
  125. if (drm_device_is_unplugged(dev))
  126. goto out;
  127. new_fops = fops_get(dev->driver->fops);
  128. if (!new_fops)
  129. goto out;
  130. replace_fops(filp, new_fops);
  131. if (filp->f_op->open)
  132. err = filp->f_op->open(inode, filp);
  133. out:
  134. mutex_unlock(&drm_global_mutex);
  135. return err;
  136. }
  137. /**
  138. * Check whether DRI will run on this CPU.
  139. *
  140. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  141. */
  142. static int drm_cpu_valid(void)
  143. {
  144. #if defined(__i386__)
  145. if (boot_cpu_data.x86 == 3)
  146. return 0; /* No cmpxchg on a 386 */
  147. #endif
  148. #if defined(__sparc__) && !defined(__sparc_v9__)
  149. return 0; /* No cmpxchg before v9 sparc. */
  150. #endif
  151. return 1;
  152. }
  153. /**
  154. * Called whenever a process opens /dev/drm.
  155. *
  156. * \param inode device inode.
  157. * \param filp file pointer.
  158. * \param dev device.
  159. * \return zero on success or a negative number on failure.
  160. *
  161. * Creates and initializes a drm_file structure for the file private data in \p
  162. * filp and add it into the double linked list in \p dev.
  163. */
  164. static int drm_open_helper(struct inode *inode, struct file *filp,
  165. struct drm_device * dev)
  166. {
  167. int minor_id = iminor(inode);
  168. struct drm_file *priv;
  169. int ret;
  170. if (filp->f_flags & O_EXCL)
  171. return -EBUSY; /* No exclusive opens */
  172. if (!drm_cpu_valid())
  173. return -EINVAL;
  174. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  175. return -EINVAL;
  176. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
  177. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  178. if (!priv)
  179. return -ENOMEM;
  180. filp->private_data = priv;
  181. priv->filp = filp;
  182. priv->uid = current_euid();
  183. priv->pid = get_pid(task_pid(current));
  184. priv->minor = idr_find(&drm_minors_idr, minor_id);
  185. if (!priv->minor) {
  186. ret = -ENODEV;
  187. goto out_put_pid;
  188. }
  189. /* for compatibility root is always authenticated */
  190. priv->always_authenticated = capable(CAP_SYS_ADMIN);
  191. priv->authenticated = priv->always_authenticated;
  192. priv->lock_count = 0;
  193. INIT_LIST_HEAD(&priv->lhead);
  194. INIT_LIST_HEAD(&priv->fbs);
  195. mutex_init(&priv->fbs_lock);
  196. INIT_LIST_HEAD(&priv->event_list);
  197. init_waitqueue_head(&priv->event_wait);
  198. priv->event_space = 4096; /* set aside 4k for event buffer */
  199. if (dev->driver->driver_features & DRIVER_GEM)
  200. drm_gem_open(dev, priv);
  201. if (drm_core_check_feature(dev, DRIVER_PRIME))
  202. drm_prime_init_file_private(&priv->prime);
  203. if (dev->driver->open) {
  204. ret = dev->driver->open(dev, priv);
  205. if (ret < 0)
  206. goto out_prime_destroy;
  207. }
  208. /* if there is no current master make this fd it, but do not create
  209. * any master object for render clients */
  210. mutex_lock(&dev->struct_mutex);
  211. if (!priv->minor->master && !drm_is_render_client(priv)) {
  212. /* create a new master */
  213. priv->minor->master = drm_master_create(priv->minor);
  214. if (!priv->minor->master) {
  215. mutex_unlock(&dev->struct_mutex);
  216. ret = -ENOMEM;
  217. goto out_close;
  218. }
  219. priv->is_master = 1;
  220. /* take another reference for the copy in the local file priv */
  221. priv->master = drm_master_get(priv->minor->master);
  222. priv->authenticated = 1;
  223. mutex_unlock(&dev->struct_mutex);
  224. if (dev->driver->master_create) {
  225. ret = dev->driver->master_create(dev, priv->master);
  226. if (ret) {
  227. mutex_lock(&dev->struct_mutex);
  228. /* drop both references if this fails */
  229. drm_master_put(&priv->minor->master);
  230. drm_master_put(&priv->master);
  231. mutex_unlock(&dev->struct_mutex);
  232. goto out_close;
  233. }
  234. }
  235. mutex_lock(&dev->struct_mutex);
  236. if (dev->driver->master_set) {
  237. ret = dev->driver->master_set(dev, priv, true);
  238. if (ret) {
  239. /* drop both references if this fails */
  240. drm_master_put(&priv->minor->master);
  241. drm_master_put(&priv->master);
  242. mutex_unlock(&dev->struct_mutex);
  243. goto out_close;
  244. }
  245. }
  246. } else if (!drm_is_render_client(priv)) {
  247. /* get a reference to the master */
  248. priv->master = drm_master_get(priv->minor->master);
  249. }
  250. mutex_unlock(&dev->struct_mutex);
  251. mutex_lock(&dev->struct_mutex);
  252. list_add(&priv->lhead, &dev->filelist);
  253. mutex_unlock(&dev->struct_mutex);
  254. #ifdef __alpha__
  255. /*
  256. * Default the hose
  257. */
  258. if (!dev->hose) {
  259. struct pci_dev *pci_dev;
  260. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  261. if (pci_dev) {
  262. dev->hose = pci_dev->sysdata;
  263. pci_dev_put(pci_dev);
  264. }
  265. if (!dev->hose) {
  266. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  267. if (b)
  268. dev->hose = b->sysdata;
  269. }
  270. }
  271. #endif
  272. return 0;
  273. out_close:
  274. if (dev->driver->postclose)
  275. dev->driver->postclose(dev, priv);
  276. out_prime_destroy:
  277. if (drm_core_check_feature(dev, DRIVER_PRIME))
  278. drm_prime_destroy_file_private(&priv->prime);
  279. if (dev->driver->driver_features & DRIVER_GEM)
  280. drm_gem_release(dev, priv);
  281. out_put_pid:
  282. put_pid(priv->pid);
  283. kfree(priv);
  284. filp->private_data = NULL;
  285. return ret;
  286. }
  287. static void drm_master_release(struct drm_device *dev, struct file *filp)
  288. {
  289. struct drm_file *file_priv = filp->private_data;
  290. if (drm_i_have_hw_lock(dev, file_priv)) {
  291. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  292. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  293. drm_lock_free(&file_priv->master->lock,
  294. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  295. }
  296. }
  297. static void drm_events_release(struct drm_file *file_priv)
  298. {
  299. struct drm_device *dev = file_priv->minor->dev;
  300. struct drm_pending_event *e, *et;
  301. struct drm_pending_vblank_event *v, *vt;
  302. unsigned long flags;
  303. spin_lock_irqsave(&dev->event_lock, flags);
  304. /* Remove pending flips */
  305. list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
  306. if (v->base.file_priv == file_priv) {
  307. list_del(&v->base.link);
  308. drm_vblank_put(dev, v->pipe);
  309. v->base.destroy(&v->base);
  310. }
  311. /* Remove unconsumed events */
  312. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  313. list_del(&e->link);
  314. e->destroy(e);
  315. }
  316. spin_unlock_irqrestore(&dev->event_lock, flags);
  317. }
  318. /**
  319. * drm_legacy_dev_reinit
  320. *
  321. * Reinitializes a legacy/ums drm device in it's lastclose function.
  322. */
  323. static void drm_legacy_dev_reinit(struct drm_device *dev)
  324. {
  325. if (drm_core_check_feature(dev, DRIVER_MODESET))
  326. return;
  327. dev->sigdata.lock = NULL;
  328. dev->context_flag = 0;
  329. dev->last_context = 0;
  330. dev->if_version = 0;
  331. }
  332. /**
  333. * Take down the DRM device.
  334. *
  335. * \param dev DRM device structure.
  336. *
  337. * Frees every resource in \p dev.
  338. *
  339. * \sa drm_device
  340. */
  341. int drm_lastclose(struct drm_device * dev)
  342. {
  343. struct drm_vma_entry *vma, *vma_temp;
  344. DRM_DEBUG("\n");
  345. if (dev->driver->lastclose)
  346. dev->driver->lastclose(dev);
  347. DRM_DEBUG("driver lastclose completed\n");
  348. if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
  349. drm_irq_uninstall(dev);
  350. mutex_lock(&dev->struct_mutex);
  351. drm_agp_clear(dev);
  352. drm_legacy_sg_cleanup(dev);
  353. /* Clear vma list (only built for debugging) */
  354. list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
  355. list_del(&vma->head);
  356. kfree(vma);
  357. }
  358. drm_legacy_dma_takedown(dev);
  359. mutex_unlock(&dev->struct_mutex);
  360. drm_legacy_dev_reinit(dev);
  361. DRM_DEBUG("lastclose completed\n");
  362. return 0;
  363. }
  364. /**
  365. * Release file.
  366. *
  367. * \param inode device inode
  368. * \param file_priv DRM file private.
  369. * \return zero on success or a negative number on failure.
  370. *
  371. * If the hardware lock is held then free it, and take it again for the kernel
  372. * context since it's necessary to reclaim buffers. Unlink the file private
  373. * data from its list and free it. Decreases the open count and if it reaches
  374. * zero calls drm_lastclose().
  375. */
  376. int drm_release(struct inode *inode, struct file *filp)
  377. {
  378. struct drm_file *file_priv = filp->private_data;
  379. struct drm_device *dev = file_priv->minor->dev;
  380. int retcode = 0;
  381. mutex_lock(&drm_global_mutex);
  382. DRM_DEBUG("open_count = %d\n", dev->open_count);
  383. if (dev->driver->preclose)
  384. dev->driver->preclose(dev, file_priv);
  385. /* ========================================================
  386. * Begin inline drm_release
  387. */
  388. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  389. task_pid_nr(current),
  390. (long)old_encode_dev(file_priv->minor->device),
  391. dev->open_count);
  392. /* Release any auth tokens that might point to this file_priv,
  393. (do that under the drm_global_mutex) */
  394. if (file_priv->magic)
  395. (void) drm_remove_magic(file_priv->master, file_priv->magic);
  396. /* if the master has gone away we can't do anything with the lock */
  397. if (file_priv->minor->master)
  398. drm_master_release(dev, filp);
  399. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  400. drm_core_reclaim_buffers(dev, file_priv);
  401. drm_events_release(file_priv);
  402. if (dev->driver->driver_features & DRIVER_MODESET)
  403. drm_fb_release(file_priv);
  404. if (dev->driver->driver_features & DRIVER_GEM)
  405. drm_gem_release(dev, file_priv);
  406. mutex_lock(&dev->ctxlist_mutex);
  407. if (!list_empty(&dev->ctxlist)) {
  408. struct drm_ctx_list *pos, *n;
  409. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  410. if (pos->tag == file_priv &&
  411. pos->handle != DRM_KERNEL_CONTEXT) {
  412. if (dev->driver->context_dtor)
  413. dev->driver->context_dtor(dev,
  414. pos->handle);
  415. drm_ctxbitmap_free(dev, pos->handle);
  416. list_del(&pos->head);
  417. kfree(pos);
  418. }
  419. }
  420. }
  421. mutex_unlock(&dev->ctxlist_mutex);
  422. mutex_lock(&dev->struct_mutex);
  423. if (file_priv->is_master) {
  424. struct drm_master *master = file_priv->master;
  425. struct drm_file *temp;
  426. list_for_each_entry(temp, &dev->filelist, lhead) {
  427. if ((temp->master == file_priv->master) &&
  428. (temp != file_priv))
  429. temp->authenticated = temp->always_authenticated;
  430. }
  431. /**
  432. * Since the master is disappearing, so is the
  433. * possibility to lock.
  434. */
  435. if (master->lock.hw_lock) {
  436. if (dev->sigdata.lock == master->lock.hw_lock)
  437. dev->sigdata.lock = NULL;
  438. master->lock.hw_lock = NULL;
  439. master->lock.file_priv = NULL;
  440. wake_up_interruptible_all(&master->lock.lock_queue);
  441. }
  442. if (file_priv->minor->master == file_priv->master) {
  443. /* drop the reference held my the minor */
  444. if (dev->driver->master_drop)
  445. dev->driver->master_drop(dev, file_priv, true);
  446. drm_master_put(&file_priv->minor->master);
  447. }
  448. }
  449. /* drop the reference held my the file priv */
  450. if (file_priv->master)
  451. drm_master_put(&file_priv->master);
  452. file_priv->is_master = 0;
  453. list_del(&file_priv->lhead);
  454. mutex_unlock(&dev->struct_mutex);
  455. if (dev->driver->postclose)
  456. dev->driver->postclose(dev, file_priv);
  457. if (drm_core_check_feature(dev, DRIVER_PRIME))
  458. drm_prime_destroy_file_private(&file_priv->prime);
  459. put_pid(file_priv->pid);
  460. kfree(file_priv);
  461. /* ========================================================
  462. * End inline drm_release
  463. */
  464. if (!--dev->open_count) {
  465. retcode = drm_lastclose(dev);
  466. if (drm_device_is_unplugged(dev))
  467. drm_put_dev(dev);
  468. }
  469. mutex_unlock(&drm_global_mutex);
  470. return retcode;
  471. }
  472. EXPORT_SYMBOL(drm_release);
  473. static bool
  474. drm_dequeue_event(struct drm_file *file_priv,
  475. size_t total, size_t max, struct drm_pending_event **out)
  476. {
  477. struct drm_device *dev = file_priv->minor->dev;
  478. struct drm_pending_event *e;
  479. unsigned long flags;
  480. bool ret = false;
  481. spin_lock_irqsave(&dev->event_lock, flags);
  482. *out = NULL;
  483. if (list_empty(&file_priv->event_list))
  484. goto out;
  485. e = list_first_entry(&file_priv->event_list,
  486. struct drm_pending_event, link);
  487. if (e->event->length + total > max)
  488. goto out;
  489. file_priv->event_space += e->event->length;
  490. list_del(&e->link);
  491. *out = e;
  492. ret = true;
  493. out:
  494. spin_unlock_irqrestore(&dev->event_lock, flags);
  495. return ret;
  496. }
  497. ssize_t drm_read(struct file *filp, char __user *buffer,
  498. size_t count, loff_t *offset)
  499. {
  500. struct drm_file *file_priv = filp->private_data;
  501. struct drm_pending_event *e;
  502. size_t total;
  503. ssize_t ret;
  504. ret = wait_event_interruptible(file_priv->event_wait,
  505. !list_empty(&file_priv->event_list));
  506. if (ret < 0)
  507. return ret;
  508. total = 0;
  509. while (drm_dequeue_event(file_priv, total, count, &e)) {
  510. if (copy_to_user(buffer + total,
  511. e->event, e->event->length)) {
  512. total = -EFAULT;
  513. break;
  514. }
  515. total += e->event->length;
  516. e->destroy(e);
  517. }
  518. return total;
  519. }
  520. EXPORT_SYMBOL(drm_read);
  521. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  522. {
  523. struct drm_file *file_priv = filp->private_data;
  524. unsigned int mask = 0;
  525. poll_wait(filp, &file_priv->event_wait, wait);
  526. if (!list_empty(&file_priv->event_list))
  527. mask |= POLLIN | POLLRDNORM;
  528. return mask;
  529. }
  530. EXPORT_SYMBOL(drm_poll);