drm_fops.c 16 KB

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