drm_fops.c 16 KB

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