drm_fops.c 17 KB

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