drm_fops.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 "drmP.h"
  36. #include <linux/poll.h>
  37. #include <linux/slab.h>
  38. /* from BKL pushdown: note that nothing else serializes idr_find() */
  39. DEFINE_MUTEX(drm_global_mutex);
  40. EXPORT_SYMBOL(drm_global_mutex);
  41. static int drm_open_helper(struct inode *inode, struct file *filp,
  42. struct drm_device * dev);
  43. static int drm_setup(struct drm_device * dev)
  44. {
  45. int i;
  46. int ret;
  47. if (dev->driver->firstopen) {
  48. ret = dev->driver->firstopen(dev);
  49. if (ret != 0)
  50. return ret;
  51. }
  52. atomic_set(&dev->ioctl_count, 0);
  53. atomic_set(&dev->vma_count, 0);
  54. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  55. !drm_core_check_feature(dev, DRIVER_MODESET)) {
  56. dev->buf_use = 0;
  57. atomic_set(&dev->buf_alloc, 0);
  58. i = drm_dma_setup(dev);
  59. if (i < 0)
  60. return i;
  61. }
  62. for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
  63. atomic_set(&dev->counts[i], 0);
  64. dev->sigdata.lock = NULL;
  65. dev->queue_count = 0;
  66. dev->queue_reserved = 0;
  67. dev->queue_slots = 0;
  68. dev->queuelist = NULL;
  69. dev->context_flag = 0;
  70. dev->interrupt_flag = 0;
  71. dev->dma_flag = 0;
  72. dev->last_context = 0;
  73. dev->last_switch = 0;
  74. dev->last_checked = 0;
  75. init_waitqueue_head(&dev->context_wait);
  76. dev->if_version = 0;
  77. dev->ctx_start = 0;
  78. dev->lck_start = 0;
  79. dev->buf_async = NULL;
  80. init_waitqueue_head(&dev->buf_readers);
  81. init_waitqueue_head(&dev->buf_writers);
  82. DRM_DEBUG("\n");
  83. /*
  84. * The kernel's context could be created here, but is now created
  85. * in drm_dma_enqueue. This is more resource-efficient for
  86. * hardware that does not do DMA, but may mean that
  87. * drm_select_queue fails between the time the interrupt is
  88. * initialized and the time the queues are initialized.
  89. */
  90. return 0;
  91. }
  92. /**
  93. * Open file.
  94. *
  95. * \param inode device inode
  96. * \param filp file pointer.
  97. * \return zero on success or a negative number on failure.
  98. *
  99. * Searches the DRM device with the same minor number, calls open_helper(), and
  100. * increments the device open count. If the open count was previous at zero,
  101. * i.e., it's the first that the device is open, then calls setup().
  102. */
  103. int drm_open(struct inode *inode, struct file *filp)
  104. {
  105. struct drm_device *dev = NULL;
  106. int minor_id = iminor(inode);
  107. struct drm_minor *minor;
  108. int retcode = 0;
  109. minor = idr_find(&drm_minors_idr, minor_id);
  110. if (!minor)
  111. return -ENODEV;
  112. if (!(dev = minor->dev))
  113. return -ENODEV;
  114. retcode = drm_open_helper(inode, filp, dev);
  115. if (!retcode) {
  116. atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
  117. if (!dev->open_count++)
  118. retcode = drm_setup(dev);
  119. }
  120. if (!retcode) {
  121. mutex_lock(&dev->struct_mutex);
  122. if (minor->type == DRM_MINOR_LEGACY) {
  123. if (dev->dev_mapping == NULL)
  124. dev->dev_mapping = inode->i_mapping;
  125. else if (dev->dev_mapping != inode->i_mapping)
  126. retcode = -ENODEV;
  127. }
  128. mutex_unlock(&dev->struct_mutex);
  129. }
  130. return retcode;
  131. }
  132. EXPORT_SYMBOL(drm_open);
  133. /**
  134. * File \c open operation.
  135. *
  136. * \param inode device inode.
  137. * \param filp file pointer.
  138. *
  139. * Puts the dev->fops corresponding to the device minor number into
  140. * \p filp, call the \c open method, and restore the file operations.
  141. */
  142. int drm_stub_open(struct inode *inode, struct file *filp)
  143. {
  144. struct drm_device *dev = NULL;
  145. struct drm_minor *minor;
  146. int minor_id = iminor(inode);
  147. int err = -ENODEV;
  148. const struct file_operations *old_fops;
  149. DRM_DEBUG("\n");
  150. mutex_lock(&drm_global_mutex);
  151. minor = idr_find(&drm_minors_idr, minor_id);
  152. if (!minor)
  153. goto out;
  154. if (!(dev = minor->dev))
  155. goto out;
  156. old_fops = filp->f_op;
  157. filp->f_op = fops_get(&dev->driver->fops);
  158. if (filp->f_op == NULL) {
  159. filp->f_op = old_fops;
  160. goto out;
  161. }
  162. if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
  163. fops_put(filp->f_op);
  164. filp->f_op = fops_get(old_fops);
  165. }
  166. fops_put(old_fops);
  167. out:
  168. mutex_unlock(&drm_global_mutex);
  169. return err;
  170. }
  171. /**
  172. * Check whether DRI will run on this CPU.
  173. *
  174. * \return non-zero if the DRI will run on this CPU, or zero otherwise.
  175. */
  176. static int drm_cpu_valid(void)
  177. {
  178. #if defined(__i386__)
  179. if (boot_cpu_data.x86 == 3)
  180. return 0; /* No cmpxchg on a 386 */
  181. #endif
  182. #if defined(__sparc__) && !defined(__sparc_v9__)
  183. return 0; /* No cmpxchg before v9 sparc. */
  184. #endif
  185. return 1;
  186. }
  187. /**
  188. * Called whenever a process opens /dev/drm.
  189. *
  190. * \param inode device inode.
  191. * \param filp file pointer.
  192. * \param dev device.
  193. * \return zero on success or a negative number on failure.
  194. *
  195. * Creates and initializes a drm_file structure for the file private data in \p
  196. * filp and add it into the double linked list in \p dev.
  197. */
  198. static int drm_open_helper(struct inode *inode, struct file *filp,
  199. struct drm_device * dev)
  200. {
  201. int minor_id = iminor(inode);
  202. struct drm_file *priv;
  203. int ret;
  204. if (filp->f_flags & O_EXCL)
  205. return -EBUSY; /* No exclusive opens */
  206. if (!drm_cpu_valid())
  207. return -EINVAL;
  208. DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
  209. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  210. if (!priv)
  211. return -ENOMEM;
  212. filp->private_data = priv;
  213. priv->filp = filp;
  214. priv->uid = current_euid();
  215. priv->pid = task_pid_nr(current);
  216. priv->minor = idr_find(&drm_minors_idr, minor_id);
  217. priv->ioctl_count = 0;
  218. /* for compatibility root is always authenticated */
  219. priv->authenticated = capable(CAP_SYS_ADMIN);
  220. priv->lock_count = 0;
  221. INIT_LIST_HEAD(&priv->lhead);
  222. INIT_LIST_HEAD(&priv->fbs);
  223. INIT_LIST_HEAD(&priv->event_list);
  224. init_waitqueue_head(&priv->event_wait);
  225. priv->event_space = 4096; /* set aside 4k for event buffer */
  226. if (dev->driver->driver_features & DRIVER_GEM)
  227. drm_gem_open(dev, priv);
  228. if (dev->driver->open) {
  229. ret = dev->driver->open(dev, priv);
  230. if (ret < 0)
  231. goto out_free;
  232. }
  233. /* if there is no current master make this fd it */
  234. mutex_lock(&dev->struct_mutex);
  235. if (!priv->minor->master) {
  236. /* create a new master */
  237. priv->minor->master = drm_master_create(priv->minor);
  238. if (!priv->minor->master) {
  239. mutex_unlock(&dev->struct_mutex);
  240. ret = -ENOMEM;
  241. goto out_free;
  242. }
  243. priv->is_master = 1;
  244. /* take another reference for the copy in the local file priv */
  245. priv->master = drm_master_get(priv->minor->master);
  246. priv->authenticated = 1;
  247. mutex_unlock(&dev->struct_mutex);
  248. if (dev->driver->master_create) {
  249. ret = dev->driver->master_create(dev, priv->master);
  250. if (ret) {
  251. mutex_lock(&dev->struct_mutex);
  252. /* drop both references if this fails */
  253. drm_master_put(&priv->minor->master);
  254. drm_master_put(&priv->master);
  255. mutex_unlock(&dev->struct_mutex);
  256. goto out_free;
  257. }
  258. }
  259. mutex_lock(&dev->struct_mutex);
  260. if (dev->driver->master_set) {
  261. ret = dev->driver->master_set(dev, priv, true);
  262. if (ret) {
  263. /* drop both references if this fails */
  264. drm_master_put(&priv->minor->master);
  265. drm_master_put(&priv->master);
  266. mutex_unlock(&dev->struct_mutex);
  267. goto out_free;
  268. }
  269. }
  270. mutex_unlock(&dev->struct_mutex);
  271. } else {
  272. /* get a reference to the master */
  273. priv->master = drm_master_get(priv->minor->master);
  274. mutex_unlock(&dev->struct_mutex);
  275. }
  276. mutex_lock(&dev->struct_mutex);
  277. list_add(&priv->lhead, &dev->filelist);
  278. mutex_unlock(&dev->struct_mutex);
  279. #ifdef __alpha__
  280. /*
  281. * Default the hose
  282. */
  283. if (!dev->hose) {
  284. struct pci_dev *pci_dev;
  285. pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  286. if (pci_dev) {
  287. dev->hose = pci_dev->sysdata;
  288. pci_dev_put(pci_dev);
  289. }
  290. if (!dev->hose) {
  291. struct pci_bus *b = pci_bus_b(pci_root_buses.next);
  292. if (b)
  293. dev->hose = b->sysdata;
  294. }
  295. }
  296. #endif
  297. return 0;
  298. out_free:
  299. kfree(priv);
  300. filp->private_data = NULL;
  301. return ret;
  302. }
  303. /** No-op. */
  304. int drm_fasync(int fd, struct file *filp, int on)
  305. {
  306. struct drm_file *priv = filp->private_data;
  307. struct drm_device *dev = priv->minor->dev;
  308. DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
  309. (long)old_encode_dev(priv->minor->device));
  310. return fasync_helper(fd, filp, on, &dev->buf_async);
  311. }
  312. EXPORT_SYMBOL(drm_fasync);
  313. /*
  314. * Reclaim locked buffers; note that this may be a bad idea if the current
  315. * context doesn't have the hw lock...
  316. */
  317. static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
  318. {
  319. struct drm_file *file_priv = f->private_data;
  320. if (drm_i_have_hw_lock(dev, file_priv)) {
  321. dev->driver->reclaim_buffers_locked(dev, file_priv);
  322. } else {
  323. unsigned long _end = jiffies + 3 * DRM_HZ;
  324. int locked = 0;
  325. drm_idlelock_take(&file_priv->master->lock);
  326. /*
  327. * Wait for a while.
  328. */
  329. do {
  330. spin_lock_bh(&file_priv->master->lock.spinlock);
  331. locked = file_priv->master->lock.idle_has_lock;
  332. spin_unlock_bh(&file_priv->master->lock.spinlock);
  333. if (locked)
  334. break;
  335. schedule();
  336. } while (!time_after_eq(jiffies, _end));
  337. if (!locked) {
  338. DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
  339. "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
  340. "\tI will go on reclaiming the buffers anyway.\n");
  341. }
  342. dev->driver->reclaim_buffers_locked(dev, file_priv);
  343. drm_idlelock_release(&file_priv->master->lock);
  344. }
  345. }
  346. static void drm_master_release(struct drm_device *dev, struct file *filp)
  347. {
  348. struct drm_file *file_priv = filp->private_data;
  349. if (dev->driver->reclaim_buffers_locked &&
  350. file_priv->master->lock.hw_lock)
  351. drm_reclaim_locked_buffers(dev, filp);
  352. if (dev->driver->reclaim_buffers_idlelocked &&
  353. file_priv->master->lock.hw_lock) {
  354. drm_idlelock_take(&file_priv->master->lock);
  355. dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
  356. drm_idlelock_release(&file_priv->master->lock);
  357. }
  358. if (drm_i_have_hw_lock(dev, file_priv)) {
  359. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  360. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  361. drm_lock_free(&file_priv->master->lock,
  362. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  363. }
  364. if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
  365. !dev->driver->reclaim_buffers_locked) {
  366. dev->driver->reclaim_buffers(dev, file_priv);
  367. }
  368. }
  369. static void drm_events_release(struct drm_file *file_priv)
  370. {
  371. struct drm_device *dev = file_priv->minor->dev;
  372. struct drm_pending_event *e, *et;
  373. struct drm_pending_vblank_event *v, *vt;
  374. unsigned long flags;
  375. spin_lock_irqsave(&dev->event_lock, flags);
  376. /* Remove pending flips */
  377. list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
  378. if (v->base.file_priv == file_priv) {
  379. list_del(&v->base.link);
  380. drm_vblank_put(dev, v->pipe);
  381. v->base.destroy(&v->base);
  382. }
  383. /* Remove unconsumed events */
  384. list_for_each_entry_safe(e, et, &file_priv->event_list, link)
  385. e->destroy(e);
  386. spin_unlock_irqrestore(&dev->event_lock, flags);
  387. }
  388. /**
  389. * Release file.
  390. *
  391. * \param inode device inode
  392. * \param file_priv DRM file private.
  393. * \return zero on success or a negative number on failure.
  394. *
  395. * If the hardware lock is held then free it, and take it again for the kernel
  396. * context since it's necessary to reclaim buffers. Unlink the file private
  397. * data from its list and free it. Decreases the open count and if it reaches
  398. * zero calls drm_lastclose().
  399. */
  400. int drm_release(struct inode *inode, struct file *filp)
  401. {
  402. struct drm_file *file_priv = filp->private_data;
  403. struct drm_device *dev = file_priv->minor->dev;
  404. int retcode = 0;
  405. mutex_lock(&drm_global_mutex);
  406. DRM_DEBUG("open_count = %d\n", dev->open_count);
  407. if (dev->driver->preclose)
  408. dev->driver->preclose(dev, file_priv);
  409. /* ========================================================
  410. * Begin inline drm_release
  411. */
  412. DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
  413. task_pid_nr(current),
  414. (long)old_encode_dev(file_priv->minor->device),
  415. dev->open_count);
  416. /* if the master has gone away we can't do anything with the lock */
  417. if (file_priv->minor->master)
  418. drm_master_release(dev, filp);
  419. drm_events_release(file_priv);
  420. if (dev->driver->driver_features & DRIVER_GEM)
  421. drm_gem_release(dev, file_priv);
  422. if (dev->driver->driver_features & DRIVER_MODESET)
  423. drm_fb_release(file_priv);
  424. mutex_lock(&dev->ctxlist_mutex);
  425. if (!list_empty(&dev->ctxlist)) {
  426. struct drm_ctx_list *pos, *n;
  427. list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
  428. if (pos->tag == file_priv &&
  429. pos->handle != DRM_KERNEL_CONTEXT) {
  430. if (dev->driver->context_dtor)
  431. dev->driver->context_dtor(dev,
  432. pos->handle);
  433. drm_ctxbitmap_free(dev, pos->handle);
  434. list_del(&pos->head);
  435. kfree(pos);
  436. --dev->ctx_count;
  437. }
  438. }
  439. }
  440. mutex_unlock(&dev->ctxlist_mutex);
  441. mutex_lock(&dev->struct_mutex);
  442. if (file_priv->is_master) {
  443. struct drm_master *master = file_priv->master;
  444. struct drm_file *temp;
  445. list_for_each_entry(temp, &dev->filelist, lhead) {
  446. if ((temp->master == file_priv->master) &&
  447. (temp != file_priv))
  448. temp->authenticated = 0;
  449. }
  450. /**
  451. * Since the master is disappearing, so is the
  452. * possibility to lock.
  453. */
  454. if (master->lock.hw_lock) {
  455. if (dev->sigdata.lock == master->lock.hw_lock)
  456. dev->sigdata.lock = NULL;
  457. master->lock.hw_lock = NULL;
  458. master->lock.file_priv = NULL;
  459. wake_up_interruptible_all(&master->lock.lock_queue);
  460. }
  461. if (file_priv->minor->master == file_priv->master) {
  462. /* drop the reference held my the minor */
  463. if (dev->driver->master_drop)
  464. dev->driver->master_drop(dev, file_priv, true);
  465. drm_master_put(&file_priv->minor->master);
  466. }
  467. }
  468. /* drop the reference held my the file priv */
  469. drm_master_put(&file_priv->master);
  470. file_priv->is_master = 0;
  471. list_del(&file_priv->lhead);
  472. mutex_unlock(&dev->struct_mutex);
  473. if (dev->driver->postclose)
  474. dev->driver->postclose(dev, file_priv);
  475. kfree(file_priv);
  476. /* ========================================================
  477. * End inline drm_release
  478. */
  479. atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
  480. if (!--dev->open_count) {
  481. if (atomic_read(&dev->ioctl_count)) {
  482. DRM_ERROR("Device busy: %d\n",
  483. atomic_read(&dev->ioctl_count));
  484. retcode = -EBUSY;
  485. } else
  486. retcode = drm_lastclose(dev);
  487. }
  488. mutex_unlock(&drm_global_mutex);
  489. return retcode;
  490. }
  491. EXPORT_SYMBOL(drm_release);
  492. static bool
  493. drm_dequeue_event(struct drm_file *file_priv,
  494. size_t total, size_t max, struct drm_pending_event **out)
  495. {
  496. struct drm_device *dev = file_priv->minor->dev;
  497. struct drm_pending_event *e;
  498. unsigned long flags;
  499. bool ret = false;
  500. spin_lock_irqsave(&dev->event_lock, flags);
  501. *out = NULL;
  502. if (list_empty(&file_priv->event_list))
  503. goto out;
  504. e = list_first_entry(&file_priv->event_list,
  505. struct drm_pending_event, link);
  506. if (e->event->length + total > max)
  507. goto out;
  508. file_priv->event_space += e->event->length;
  509. list_del(&e->link);
  510. *out = e;
  511. ret = true;
  512. out:
  513. spin_unlock_irqrestore(&dev->event_lock, flags);
  514. return ret;
  515. }
  516. ssize_t drm_read(struct file *filp, char __user *buffer,
  517. size_t count, loff_t *offset)
  518. {
  519. struct drm_file *file_priv = filp->private_data;
  520. struct drm_pending_event *e;
  521. size_t total;
  522. ssize_t ret;
  523. ret = wait_event_interruptible(file_priv->event_wait,
  524. !list_empty(&file_priv->event_list));
  525. if (ret < 0)
  526. return ret;
  527. total = 0;
  528. while (drm_dequeue_event(file_priv, total, count, &e)) {
  529. if (copy_to_user(buffer + total,
  530. e->event, e->event->length)) {
  531. total = -EFAULT;
  532. break;
  533. }
  534. total += e->event->length;
  535. e->destroy(e);
  536. }
  537. return total;
  538. }
  539. EXPORT_SYMBOL(drm_read);
  540. unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
  541. {
  542. struct drm_file *file_priv = filp->private_data;
  543. unsigned int mask = 0;
  544. poll_wait(filp, &file_priv->event_wait, wait);
  545. if (!list_empty(&file_priv->event_list))
  546. mask |= POLLIN | POLLRDNORM;
  547. return mask;
  548. }
  549. EXPORT_SYMBOL(drm_poll);