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