drm_fops.c 16 KB

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