drm_fops.c 16 KB

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