drm_fops.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 */
  40. DEFINE_MUTEX(drm_global_mutex);
  41. EXPORT_SYMBOL(drm_global_mutex);
  42. static int drm_open_helper(struct file *filp, struct drm_minor *minor);
  43. static int drm_setup(struct drm_device * dev)
  44. {
  45. int ret;
  46. if (dev->driver->firstopen &&
  47. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  48. ret = dev->driver->firstopen(dev);
  49. if (ret != 0)
  50. return ret;
  51. }
  52. ret = drm_legacy_dma_setup(dev);
  53. if (ret < 0)
  54. return ret;
  55. DRM_DEBUG("\n");
  56. return 0;
  57. }
  58. /**
  59. * Open file.
  60. *
  61. * \param inode device inode
  62. * \param filp file pointer.
  63. * \return zero on success or a negative number on failure.
  64. *
  65. * Searches the DRM device with the same minor number, calls open_helper(), and
  66. * increments the device open count. If the open count was previous at zero,
  67. * i.e., it's the first that the device is open, then calls setup().
  68. */
  69. int drm_open(struct inode *inode, struct file *filp)
  70. {
  71. struct drm_device *dev;
  72. struct drm_minor *minor;
  73. int retcode;
  74. int need_setup = 0;
  75. minor = drm_minor_acquire(iminor(inode));
  76. if (IS_ERR(minor))
  77. return PTR_ERR(minor);
  78. dev = minor->dev;
  79. if (!dev->open_count++)
  80. need_setup = 1;
  81. /* share address_space across all char-devs of a single device */
  82. filp->f_mapping = dev->anon_inode->i_mapping;
  83. retcode = drm_open_helper(filp, minor);
  84. if (retcode)
  85. goto err_undo;
  86. if (need_setup) {
  87. retcode = drm_setup(dev);
  88. if (retcode)
  89. goto err_undo;
  90. }
  91. return 0;
  92. err_undo:
  93. dev->open_count--;
  94. drm_minor_release(minor);
  95. return retcode;
  96. }
  97. EXPORT_SYMBOL(drm_open);
  98. /**
  99. * File \c open operation.
  100. *
  101. * \param inode device inode.
  102. * \param filp file pointer.
  103. *
  104. * Puts the dev->fops corresponding to the device minor number into
  105. * \p filp, call the \c open method, and restore the file operations.
  106. */
  107. int drm_stub_open(struct inode *inode, struct file *filp)
  108. {
  109. struct drm_device *dev;
  110. struct drm_minor *minor;
  111. int err = -ENODEV;
  112. const struct file_operations *new_fops;
  113. DRM_DEBUG("\n");
  114. mutex_lock(&drm_global_mutex);
  115. minor = drm_minor_acquire(iminor(inode));
  116. if (IS_ERR(minor))
  117. goto out_unlock;
  118. dev = minor->dev;
  119. new_fops = fops_get(dev->driver->fops);
  120. if (!new_fops)
  121. goto out_release;
  122. replace_fops(filp, new_fops);
  123. if (filp->f_op->open)
  124. err = filp->f_op->open(inode, filp);
  125. out_release:
  126. drm_minor_release(minor);
  127. out_unlock:
  128. mutex_unlock(&drm_global_mutex);
  129. return err;
  130. }
  131. /**
  132. * Check whether DRI will run on this CPU.
  133. *
  134. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  135. */
  136. static int drm_cpu_valid(void)
  137. {
  138. #if defined(__sparc__) && !defined(__sparc_v9__)
  139. return 0; /* No cmpxchg before v9 sparc. */
  140. #endif
  141. return 1;
  142. }
  143. /**
  144. * Called whenever a process opens /dev/drm.
  145. *
  146. * \param filp file pointer.
  147. * \param minor acquired minor-object.
  148. * \return zero on success or a negative number on failure.
  149. *
  150. * Creates and initializes a drm_file structure for the file private data in \p
  151. * filp and add it into the double linked list in \p dev.
  152. */
  153. static int drm_open_helper(struct file *filp, struct drm_minor *minor)
  154. {
  155. struct drm_device *dev = minor->dev;
  156. struct drm_file *priv;
  157. int ret;
  158. if (filp->f_flags & O_EXCL)
  159. return -EBUSY; /* No exclusive opens */
  160. if (!drm_cpu_valid())
  161. return -EINVAL;
  162. if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
  163. return -EINVAL;
  164. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
  165. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  166. if (!priv)
  167. return -ENOMEM;
  168. filp->private_data = priv;
  169. priv->filp = filp;
  170. priv->uid = current_euid();
  171. priv->pid = get_pid(task_pid(current));
  172. priv->minor = minor;
  173. /* for compatibility root is always authenticated */
  174. priv->always_authenticated = capable(CAP_SYS_ADMIN);
  175. priv->authenticated = priv->always_authenticated;
  176. priv->lock_count = 0;
  177. INIT_LIST_HEAD(&priv->lhead);
  178. INIT_LIST_HEAD(&priv->fbs);
  179. mutex_init(&priv->fbs_lock);
  180. INIT_LIST_HEAD(&priv->event_list);
  181. init_waitqueue_head(&priv->event_wait);
  182. priv->event_space = 4096; /* set aside 4k for event buffer */
  183. if (dev->driver->driver_features & DRIVER_GEM)
  184. drm_gem_open(dev, priv);
  185. if (drm_core_check_feature(dev, DRIVER_PRIME))
  186. drm_prime_init_file_private(&priv->prime);
  187. if (dev->driver->open) {
  188. ret = dev->driver->open(dev, priv);
  189. if (ret < 0)
  190. goto out_prime_destroy;
  191. }
  192. /* if there is no current master make this fd it, but do not create
  193. * any master object for render clients */
  194. mutex_lock(&dev->master_mutex);
  195. if (drm_is_primary_client(priv) && !priv->minor->master) {
  196. /* create a new master */
  197. priv->minor->master = drm_master_create(priv->minor);
  198. if (!priv->minor->master) {
  199. ret = -ENOMEM;
  200. goto out_close;
  201. }
  202. /* take another reference for the copy in the local file priv */
  203. priv->master = drm_master_get(priv->minor->master);
  204. priv->authenticated = 1;
  205. if (dev->driver->master_create) {
  206. ret = dev->driver->master_create(dev, priv->master);
  207. if (ret) {
  208. /* drop both references if this fails */
  209. drm_master_put(&priv->minor->master);
  210. drm_master_put(&priv->master);
  211. goto out_close;
  212. }
  213. }
  214. if (dev->driver->master_set) {
  215. ret = dev->driver->master_set(dev, priv, true);
  216. if (ret) {
  217. /* drop both references if this fails */
  218. drm_master_put(&priv->minor->master);
  219. drm_master_put(&priv->master);
  220. goto out_close;
  221. }
  222. }
  223. } else if (drm_is_primary_client(priv)) {
  224. /* get a reference to the master */
  225. priv->master = drm_master_get(priv->minor->master);
  226. }
  227. mutex_unlock(&dev->master_mutex);
  228. mutex_lock(&dev->struct_mutex);
  229. list_add(&priv->lhead, &dev->filelist);
  230. mutex_unlock(&dev->struct_mutex);
  231. #ifdef __alpha__
  232. /*
  233. * Default the hose
  234. */
  235. if (!dev->hose) {
  236. struct pci_dev *pci_dev;
  237. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  238. if (pci_dev) {
  239. dev->hose = pci_dev->sysdata;
  240. pci_dev_put(pci_dev);
  241. }
  242. if (!dev->hose) {
  243. struct pci_bus *b = list_entry(pci_root_buses.next,
  244. struct pci_bus, node);
  245. if (b)
  246. dev->hose = b->sysdata;
  247. }
  248. }
  249. #endif
  250. return 0;
  251. out_close:
  252. mutex_unlock(&dev->master_mutex);
  253. if (dev->driver->postclose)
  254. dev->driver->postclose(dev, priv);
  255. out_prime_destroy:
  256. if (drm_core_check_feature(dev, DRIVER_PRIME))
  257. drm_prime_destroy_file_private(&priv->prime);
  258. if (dev->driver->driver_features & DRIVER_GEM)
  259. drm_gem_release(dev, priv);
  260. put_pid(priv->pid);
  261. kfree(priv);
  262. filp->private_data = NULL;
  263. return ret;
  264. }
  265. static void drm_master_release(struct drm_device *dev, struct file *filp)
  266. {
  267. struct drm_file *file_priv = filp->private_data;
  268. if (drm_i_have_hw_lock(dev, file_priv)) {
  269. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  270. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  271. drm_lock_free(&file_priv->master->lock,
  272. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  273. }
  274. }
  275. static void drm_events_release(struct drm_file *file_priv)
  276. {
  277. struct drm_device *dev = file_priv->minor->dev;
  278. struct drm_pending_event *e, *et;
  279. struct drm_pending_vblank_event *v, *vt;
  280. unsigned long flags;
  281. spin_lock_irqsave(&dev->event_lock, flags);
  282. /* Remove pending flips */
  283. list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
  284. if (v->base.file_priv == file_priv) {
  285. list_del(&v->base.link);
  286. drm_vblank_put(dev, v->pipe);
  287. v->base.destroy(&v->base);
  288. }
  289. /* Remove unconsumed events */
  290. list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
  291. list_del(&e->link);
  292. e->destroy(e);
  293. }
  294. spin_unlock_irqrestore(&dev->event_lock, flags);
  295. }
  296. /**
  297. * drm_legacy_dev_reinit
  298. *
  299. * Reinitializes a legacy/ums drm device in it's lastclose function.
  300. */
  301. static void drm_legacy_dev_reinit(struct drm_device *dev)
  302. {
  303. if (drm_core_check_feature(dev, DRIVER_MODESET))
  304. return;
  305. dev->sigdata.lock = NULL;
  306. dev->context_flag = 0;
  307. dev->last_context = 0;
  308. dev->if_version = 0;
  309. }
  310. /**
  311. * Take down the DRM device.
  312. *
  313. * \param dev DRM device structure.
  314. *
  315. * Frees every resource in \p dev.
  316. *
  317. * \sa drm_device
  318. */
  319. int drm_lastclose(struct drm_device * dev)
  320. {
  321. struct drm_vma_entry *vma, *vma_temp;
  322. DRM_DEBUG("\n");
  323. if (dev->driver->lastclose)
  324. dev->driver->lastclose(dev);
  325. DRM_DEBUG("driver lastclose completed\n");
  326. if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
  327. drm_irq_uninstall(dev);
  328. mutex_lock(&dev->struct_mutex);
  329. drm_agp_clear(dev);
  330. drm_legacy_sg_cleanup(dev);
  331. /* Clear vma list (only built for debugging) */
  332. list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
  333. list_del(&vma->head);
  334. kfree(vma);
  335. }
  336. drm_legacy_dma_takedown(dev);
  337. mutex_unlock(&dev->struct_mutex);
  338. drm_legacy_dev_reinit(dev);
  339. DRM_DEBUG("lastclose completed\n");
  340. return 0;
  341. }
  342. /**
  343. * Release file.
  344. *
  345. * \param inode device inode
  346. * \param file_priv DRM file private.
  347. * \return zero on success or a negative number on failure.
  348. *
  349. * If the hardware lock is held then free it, and take it again for the kernel
  350. * context since it's necessary to reclaim buffers. Unlink the file private
  351. * data from its list and free it. Decreases the open count and if it reaches
  352. * zero calls drm_lastclose().
  353. */
  354. int drm_release(struct inode *inode, struct file *filp)
  355. {
  356. struct drm_file *file_priv = filp->private_data;
  357. struct drm_minor *minor = file_priv->minor;
  358. struct drm_device *dev = minor->dev;
  359. int retcode = 0;
  360. mutex_lock(&drm_global_mutex);
  361. DRM_DEBUG("open_count = %d\n", dev->open_count);
  362. if (dev->driver->preclose)
  363. dev->driver->preclose(dev, file_priv);
  364. /* ========================================================
  365. * Begin inline drm_release
  366. */
  367. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  368. task_pid_nr(current),
  369. (long)old_encode_dev(file_priv->minor->kdev->devt),
  370. dev->open_count);
  371. /* Release any auth tokens that might point to this file_priv,
  372. (do that under the drm_global_mutex) */
  373. if (file_priv->magic)
  374. (void) drm_remove_magic(file_priv->master, file_priv->magic);
  375. /* if the master has gone away we can't do anything with the lock */
  376. if (file_priv->minor->master)
  377. drm_master_release(dev, filp);
  378. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
  379. drm_core_reclaim_buffers(dev, file_priv);
  380. drm_events_release(file_priv);
  381. if (dev->driver->driver_features & DRIVER_MODESET)
  382. drm_fb_release(file_priv);
  383. if (dev->driver->driver_features & DRIVER_GEM)
  384. drm_gem_release(dev, file_priv);
  385. drm_ctxbitmap_flush(dev, file_priv);
  386. mutex_lock(&dev->master_mutex);
  387. if (drm_is_master(file_priv)) {
  388. struct drm_master *master = file_priv->master;
  389. struct drm_file *temp;
  390. mutex_lock(&dev->struct_mutex);
  391. list_for_each_entry(temp, &dev->filelist, lhead) {
  392. if ((temp->master == file_priv->master) &&
  393. (temp != file_priv))
  394. temp->authenticated = temp->always_authenticated;
  395. }
  396. /**
  397. * Since the master is disappearing, so is the
  398. * possibility to lock.
  399. */
  400. if (master->lock.hw_lock) {
  401. if (dev->sigdata.lock == master->lock.hw_lock)
  402. dev->sigdata.lock = NULL;
  403. master->lock.hw_lock = NULL;
  404. master->lock.file_priv = NULL;
  405. wake_up_interruptible_all(&master->lock.lock_queue);
  406. }
  407. mutex_unlock(&dev->struct_mutex);
  408. if (file_priv->minor->master == file_priv->master) {
  409. /* drop the reference held my the minor */
  410. if (dev->driver->master_drop)
  411. dev->driver->master_drop(dev, file_priv, true);
  412. drm_master_put(&file_priv->minor->master);
  413. }
  414. }
  415. /* drop the master reference held by the file priv */
  416. if (file_priv->master)
  417. drm_master_put(&file_priv->master);
  418. mutex_unlock(&dev->master_mutex);
  419. mutex_lock(&dev->struct_mutex);
  420. list_del(&file_priv->lhead);
  421. mutex_unlock(&dev->struct_mutex);
  422. if (dev->driver->postclose)
  423. dev->driver->postclose(dev, file_priv);
  424. if (drm_core_check_feature(dev, DRIVER_PRIME))
  425. drm_prime_destroy_file_private(&file_priv->prime);
  426. put_pid(file_priv->pid);
  427. kfree(file_priv);
  428. /* ========================================================
  429. * End inline drm_release
  430. */
  431. if (!--dev->open_count) {
  432. retcode = drm_lastclose(dev);
  433. if (drm_device_is_unplugged(dev))
  434. drm_put_dev(dev);
  435. }
  436. mutex_unlock(&drm_global_mutex);
  437. drm_minor_release(minor);
  438. return retcode;
  439. }
  440. EXPORT_SYMBOL(drm_release);
  441. static bool
  442. drm_dequeue_event(struct drm_file *file_priv,
  443. size_t total, size_t max, struct drm_pending_event **out)
  444. {
  445. struct drm_device *dev = file_priv->minor->dev;
  446. struct drm_pending_event *e;
  447. unsigned long flags;
  448. bool ret = false;
  449. spin_lock_irqsave(&dev->event_lock, flags);
  450. *out = NULL;
  451. if (list_empty(&file_priv->event_list))
  452. goto out;
  453. e = list_first_entry(&file_priv->event_list,
  454. struct drm_pending_event, link);
  455. if (e->event->length + total > max)
  456. goto out;
  457. file_priv->event_space += e->event->length;
  458. list_del(&e->link);
  459. *out = e;
  460. ret = true;
  461. out:
  462. spin_unlock_irqrestore(&dev->event_lock, flags);
  463. return ret;
  464. }
  465. ssize_t drm_read(struct file *filp, char __user *buffer,
  466. size_t count, loff_t *offset)
  467. {
  468. struct drm_file *file_priv = filp->private_data;
  469. struct drm_pending_event *e;
  470. size_t total;
  471. ssize_t ret;
  472. ret = wait_event_interruptible(file_priv->event_wait,
  473. !list_empty(&file_priv->event_list));
  474. if (ret < 0)
  475. return ret;
  476. total = 0;
  477. while (drm_dequeue_event(file_priv, total, count, &e)) {
  478. if (copy_to_user(buffer + total,
  479. e->event, e->event->length)) {
  480. total = -EFAULT;
  481. break;
  482. }
  483. total += e->event->length;
  484. e->destroy(e);
  485. }
  486. return total;
  487. }
  488. EXPORT_SYMBOL(drm_read);
  489. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  490. {
  491. struct drm_file *file_priv = filp->private_data;
  492. unsigned int mask = 0;
  493. poll_wait(filp, &file_priv->event_wait, wait);
  494. if (!list_empty(&file_priv->event_list))
  495. mask |= POLLIN | POLLRDNORM;
  496. return mask;
  497. }
  498. EXPORT_SYMBOL(drm_poll);