drm_fops.c 16 KB

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