Subversion Repositories Kolibri OS

Rev

Rev 6320 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
  3.  *
  4.  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
  5.  * All Rights Reserved.
  6.  *
  7.  * Author Rickard E. (Rik) Faith <faith@valinux.com>
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice (including the next
  17.  * paragraph) shall be included in all copies or substantial portions of the
  18.  * Software.
  19.  *
  20.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  23.  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26.  * DEALINGS IN THE SOFTWARE.
  27.  */
  28.  
  29. #include <linux/fs.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/slab.h>
  33. #include <drm/drmP.h>
  34. #include <drm/drm_core.h>
  35. #include "drm_internal.h"
  36.  
  37. unsigned int drm_debug = 0;     /* 1 to enable debug output */
  38. EXPORT_SYMBOL(drm_debug);
  39.  
  40. unsigned int drm_rnodes = 0;    /* 1 to enable experimental render nodes API */
  41. EXPORT_SYMBOL(drm_rnodes);
  42.  
  43. /* 1 to allow user space to request universal planes (experimental) */
  44. unsigned int drm_universal_planes = 0;
  45. EXPORT_SYMBOL(drm_universal_planes);
  46.  
  47. unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
  48. EXPORT_SYMBOL(drm_vblank_offdelay);
  49.  
  50. unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
  51. EXPORT_SYMBOL(drm_timestamp_precision);
  52.  
  53. struct idr drm_minors_idr;
  54.  
  55. void drm_err(const char *format, ...)
  56. {
  57.     struct va_format vaf;
  58.     va_list args;
  59.  
  60.     va_start(args, format);
  61.  
  62.     vaf.fmt = format;
  63.     vaf.va = &args;
  64.  
  65.     printk(KERN_ERR "[" DRM_NAME ":%pf] *ERROR* %pV",
  66.            __builtin_return_address(0), &vaf);
  67.  
  68.     va_end(args);
  69. }
  70. EXPORT_SYMBOL(drm_err);
  71.  
  72. void drm_ut_debug_printk(const char *function_name, const char *format, ...)
  73. {
  74.         struct va_format vaf;
  75.         va_list args;
  76.  
  77. //   if (drm_debug & request_level) {
  78. //       if (function_name)
  79. //           printk(KERN_DEBUG "[%s:%s], ", prefix, function_name);
  80. //       va_start(args, format);
  81. //       vprintk(format, args);
  82. //       va_end(args);
  83. //   }
  84. }
  85. EXPORT_SYMBOL(drm_ut_debug_printk);
  86.  
  87. #if 0
  88. struct drm_master *drm_master_create(struct drm_minor *minor)
  89. {
  90.         struct drm_master *master;
  91.  
  92.         master = kzalloc(sizeof(*master), GFP_KERNEL);
  93.         if (!master)
  94.                 return NULL;
  95.  
  96.         kref_init(&master->refcount);
  97.         spin_lock_init(&master->lock.spinlock);
  98.         init_waitqueue_head(&master->lock.lock_queue);
  99.         if (drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER)) {
  100.                 kfree(master);
  101.                 return NULL;
  102.         }
  103.         INIT_LIST_HEAD(&master->magicfree);
  104.         master->minor = minor;
  105.  
  106.         return master;
  107. }
  108.  
  109. struct drm_master *drm_master_get(struct drm_master *master)
  110. {
  111.         kref_get(&master->refcount);
  112.         return master;
  113. }
  114. EXPORT_SYMBOL(drm_master_get);
  115.  
  116. static void drm_master_destroy(struct kref *kref)
  117. {
  118.         struct drm_master *master = container_of(kref, struct drm_master, refcount);
  119.         struct drm_magic_entry *pt, *next;
  120.         struct drm_device *dev = master->minor->dev;
  121.         struct drm_map_list *r_list, *list_temp;
  122.  
  123.         mutex_lock(&dev->struct_mutex);
  124.         if (dev->driver->master_destroy)
  125.                 dev->driver->master_destroy(dev, master);
  126.  
  127.         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
  128.                 if (r_list->master == master) {
  129.                         drm_rmmap_locked(dev, r_list->map);
  130.                         r_list = NULL;
  131.                 }
  132.         }
  133.  
  134.         if (master->unique) {
  135.                 kfree(master->unique);
  136.                 master->unique = NULL;
  137.                 master->unique_len = 0;
  138.         }
  139.  
  140.         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
  141.                 list_del(&pt->head);
  142.                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
  143.                 kfree(pt);
  144.         }
  145.  
  146.         drm_ht_remove(&master->magiclist);
  147.  
  148.         mutex_unlock(&dev->struct_mutex);
  149.         kfree(master);
  150. }
  151.  
  152. void drm_master_put(struct drm_master **master)
  153. {
  154.         kref_put(&(*master)->refcount, drm_master_destroy);
  155.         *master = NULL;
  156. }
  157. EXPORT_SYMBOL(drm_master_put);
  158.  
  159. int drm_setmaster_ioctl(struct drm_device *dev, void *data,
  160.                         struct drm_file *file_priv)
  161. {
  162.         int ret = 0;
  163.  
  164.         mutex_lock(&dev->master_mutex);
  165.         if (file_priv->is_master)
  166.                 goto out_unlock;
  167.  
  168.         if (file_priv->minor->master) {
  169.                 ret = -EINVAL;
  170.                 goto out_unlock;
  171.         }
  172.  
  173.         if (!file_priv->master) {
  174.                 ret = -EINVAL;
  175.                 goto out_unlock;
  176.         }
  177.  
  178.         file_priv->minor->master = drm_master_get(file_priv->master);
  179.         file_priv->is_master = 1;
  180.         if (dev->driver->master_set) {
  181.                 ret = dev->driver->master_set(dev, file_priv, false);
  182.                 if (unlikely(ret != 0)) {
  183.                         file_priv->is_master = 0;
  184.                         drm_master_put(&file_priv->minor->master);
  185.                 }
  186.         }
  187.  
  188. out_unlock:
  189.         mutex_unlock(&dev->master_mutex);
  190.         return ret;
  191. }
  192.  
  193. int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
  194.                          struct drm_file *file_priv)
  195. {
  196.         int ret = -EINVAL;
  197.  
  198.         mutex_lock(&dev->master_mutex);
  199.         if (!file_priv->is_master)
  200.                 goto out_unlock;
  201.  
  202.         if (!file_priv->minor->master)
  203.                 goto out_unlock;
  204.  
  205.         ret = 0;
  206.         if (dev->driver->master_drop)
  207.                 dev->driver->master_drop(dev, file_priv, false);
  208.         drm_master_put(&file_priv->minor->master);
  209.         file_priv->is_master = 0;
  210.  
  211. out_unlock:
  212.         mutex_unlock(&dev->master_mutex);
  213.         return ret;
  214. }
  215.  
  216. /*
  217.  * DRM Minors
  218.  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
  219.  * of them is represented by a drm_minor object. Depending on the capabilities
  220.  * of the device-driver, different interfaces are registered.
  221.  *
  222.  * Minors can be accessed via dev->$minor_name. This pointer is either
  223.  * NULL or a valid drm_minor pointer and stays valid as long as the device is
  224.  * valid. This means, DRM minors have the same life-time as the underlying
  225.  * device. However, this doesn't mean that the minor is active. Minors are
  226.  * registered and unregistered dynamically according to device-state.
  227.  */
  228.  
  229. static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
  230.                                              unsigned int type)
  231. {
  232.         switch (type) {
  233.         case DRM_MINOR_LEGACY:
  234.                 return &dev->primary;
  235.         case DRM_MINOR_RENDER:
  236.                 return &dev->render;
  237.         case DRM_MINOR_CONTROL:
  238.                 return &dev->control;
  239.         default:
  240.                 return NULL;
  241.         }
  242. }
  243.  
  244. static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
  245. {
  246.         struct drm_minor *minor;
  247.  
  248.         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
  249.         if (!minor)
  250.                 return -ENOMEM;
  251.  
  252.         minor->type = type;
  253.         minor->dev = dev;
  254.  
  255.         *drm_minor_get_slot(dev, type) = minor;
  256.         return 0;
  257. }
  258.  
  259. static void drm_minor_free(struct drm_device *dev, unsigned int type)
  260. {
  261.         struct drm_minor **slot;
  262.  
  263.         slot = drm_minor_get_slot(dev, type);
  264.         if (*slot) {
  265.                 drm_mode_group_destroy(&(*slot)->mode_group);
  266.                 kfree(*slot);
  267.                 *slot = NULL;
  268.         }
  269. }
  270.  
  271. static int drm_minor_register(struct drm_device *dev, unsigned int type)
  272. {
  273.         struct drm_minor *new_minor;
  274.         unsigned long flags;
  275.         int ret;
  276.         int minor_id;
  277.  
  278.         DRM_DEBUG("\n");
  279.  
  280.         new_minor = *drm_minor_get_slot(dev, type);
  281.         if (!new_minor)
  282.                 return 0;
  283.  
  284.         idr_preload(GFP_KERNEL);
  285.         spin_lock_irqsave(&drm_minor_lock, flags);
  286.         minor_id = idr_alloc(&drm_minors_idr,
  287.                              NULL,
  288.                              64 * type,
  289.                              64 * (type + 1),
  290.                              GFP_NOWAIT);
  291.         spin_unlock_irqrestore(&drm_minor_lock, flags);
  292.         idr_preload_end();
  293.  
  294.         if (minor_id < 0)
  295.                 return minor_id;
  296.  
  297.         new_minor->index = minor_id;
  298.  
  299.         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
  300.         if (ret) {
  301.                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
  302.                 goto err_id;
  303.         }
  304.  
  305.         ret = drm_sysfs_device_add(new_minor);
  306.         if (ret) {
  307.                 DRM_ERROR("DRM: Error sysfs_device_add.\n");
  308.                 goto err_debugfs;
  309.         }
  310.  
  311.         /* replace NULL with @minor so lookups will succeed from now on */
  312.         spin_lock_irqsave(&drm_minor_lock, flags);
  313.         idr_replace(&drm_minors_idr, new_minor, new_minor->index);
  314.         spin_unlock_irqrestore(&drm_minor_lock, flags);
  315.  
  316.         DRM_DEBUG("new minor assigned %d\n", minor_id);
  317.         return 0;
  318.  
  319. err_debugfs:
  320.         drm_debugfs_cleanup(new_minor);
  321. err_id:
  322.         spin_lock_irqsave(&drm_minor_lock, flags);
  323.         idr_remove(&drm_minors_idr, minor_id);
  324.         spin_unlock_irqrestore(&drm_minor_lock, flags);
  325.         new_minor->index = 0;
  326.         return ret;
  327. }
  328.  
  329. static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
  330. {
  331.         struct drm_minor *minor;
  332.         unsigned long flags;
  333.  
  334.         minor = *drm_minor_get_slot(dev, type);
  335.         if (!minor || !minor->kdev)
  336.                 return;
  337.  
  338.         spin_lock_irqsave(&drm_minor_lock, flags);
  339.         idr_remove(&drm_minors_idr, minor->index);
  340.         spin_unlock_irqrestore(&drm_minor_lock, flags);
  341.         minor->index = 0;
  342.  
  343.         drm_debugfs_cleanup(minor);
  344.         drm_sysfs_device_remove(minor);
  345. }
  346.  
  347. /**
  348.  * drm_minor_acquire - Acquire a DRM minor
  349.  * @minor_id: Minor ID of the DRM-minor
  350.  *
  351.  * Looks up the given minor-ID and returns the respective DRM-minor object. The
  352.  * refence-count of the underlying device is increased so you must release this
  353.  * object with drm_minor_release().
  354.  *
  355.  * As long as you hold this minor, it is guaranteed that the object and the
  356.  * minor->dev pointer will stay valid! However, the device may get unplugged and
  357.  * unregistered while you hold the minor.
  358.  *
  359.  * Returns:
  360.  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
  361.  * failure.
  362.  */
  363. struct drm_minor *drm_minor_acquire(unsigned int minor_id)
  364. {
  365.         struct drm_minor *minor;
  366.         unsigned long flags;
  367.  
  368.         spin_lock_irqsave(&drm_minor_lock, flags);
  369.         minor = idr_find(&drm_minors_idr, minor_id);
  370.         if (minor)
  371.                 drm_dev_ref(minor->dev);
  372.         spin_unlock_irqrestore(&drm_minor_lock, flags);
  373.  
  374.         if (!minor) {
  375.                 return ERR_PTR(-ENODEV);
  376.         } else if (drm_device_is_unplugged(minor->dev)) {
  377.                 drm_dev_unref(minor->dev);
  378.                 return ERR_PTR(-ENODEV);
  379.         }
  380.  
  381.         return minor;
  382. }
  383.  
  384. /**
  385.  * drm_minor_release - Release DRM minor
  386.  * @minor: Pointer to DRM minor object
  387.  *
  388.  * Release a minor that was previously acquired via drm_minor_acquire().
  389.  */
  390. void drm_minor_release(struct drm_minor *minor)
  391. {
  392.         drm_dev_unref(minor->dev);
  393. }
  394.  
  395. /**
  396.  * drm_put_dev - Unregister and release a DRM device
  397.  * @dev: DRM device
  398.  *
  399.  * Called at module unload time or when a PCI device is unplugged.
  400.  *
  401.  * Use of this function is discouraged. It will eventually go away completely.
  402.  * Please use drm_dev_unregister() and drm_dev_unref() explicitly instead.
  403.  *
  404.  * Cleans up all DRM device, calling drm_lastclose().
  405.  */
  406. void drm_put_dev(struct drm_device *dev)
  407. {
  408.         DRM_DEBUG("\n");
  409.  
  410.         if (!dev) {
  411.                 DRM_ERROR("cleanup called no dev\n");
  412.                 return;
  413.         }
  414.  
  415.         drm_dev_unregister(dev);
  416.         drm_dev_unref(dev);
  417. }
  418. EXPORT_SYMBOL(drm_put_dev);
  419.  
  420. void drm_unplug_dev(struct drm_device *dev)
  421. {
  422.         /* for a USB device */
  423.         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
  424.         drm_minor_unregister(dev, DRM_MINOR_RENDER);
  425.         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
  426.  
  427.         mutex_lock(&drm_global_mutex);
  428.  
  429.         drm_device_set_unplugged(dev);
  430.  
  431.         if (dev->open_count == 0) {
  432.                 drm_put_dev(dev);
  433.         }
  434.         mutex_unlock(&drm_global_mutex);
  435. }
  436. EXPORT_SYMBOL(drm_unplug_dev);
  437.  
  438. /*
  439.  * DRM internal mount
  440.  * We want to be able to allocate our own "struct address_space" to control
  441.  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
  442.  * stand-alone address_space objects, so we need an underlying inode. As there
  443.  * is no way to allocate an independent inode easily, we need a fake internal
  444.  * VFS mount-point.
  445.  *
  446.  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
  447.  * frees it again. You are allowed to use iget() and iput() to get references to
  448.  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
  449.  * drm_fs_inode_free() call (which does not have to be the last iput()).
  450.  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
  451.  * between multiple inode-users. You could, technically, call
  452.  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
  453.  * iput(), but this way you'd end up with a new vfsmount for each inode.
  454.  */
  455.  
  456. static int drm_fs_cnt;
  457. static struct vfsmount *drm_fs_mnt;
  458.  
  459. static const struct dentry_operations drm_fs_dops = {
  460.         .d_dname        = simple_dname,
  461. };
  462.  
  463. static const struct super_operations drm_fs_sops = {
  464.         .statfs         = simple_statfs,
  465. };
  466.  
  467. static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
  468.                                    const char *dev_name, void *data)
  469. {
  470.         return mount_pseudo(fs_type,
  471.                             "drm:",
  472.                             &drm_fs_sops,
  473.                             &drm_fs_dops,
  474.                             0x010203ff);
  475. }
  476.  
  477. static struct file_system_type drm_fs_type = {
  478.         .name           = "drm",
  479.         .owner          = THIS_MODULE,
  480.         .mount          = drm_fs_mount,
  481.         .kill_sb        = kill_anon_super,
  482. };
  483.  
  484. #endif
  485.  
  486.  
  487.  
  488.  
  489.  
  490. int drm_fill_in_dev(struct drm_device *dev,
  491.                            const struct pci_device_id *ent,
  492.                            struct drm_driver *driver)
  493. {
  494.         int ret;
  495.         dev->driver = driver;
  496.  
  497.         INIT_LIST_HEAD(&dev->filelist);
  498.         INIT_LIST_HEAD(&dev->ctxlist);
  499.         INIT_LIST_HEAD(&dev->vmalist);
  500.         INIT_LIST_HEAD(&dev->maplist);
  501.         INIT_LIST_HEAD(&dev->vblank_event_list);
  502.  
  503.         spin_lock_init(&dev->buf_lock);
  504.         spin_lock_init(&dev->event_lock);
  505.         mutex_init(&dev->struct_mutex);
  506.         mutex_init(&dev->ctxlist_mutex);
  507.  
  508. //      if (drm_ht_create(&dev->map_hash, 12)) {
  509. //              return -ENOMEM;
  510. //      }
  511.  
  512.  
  513.  
  514.         if (driver->driver_features & DRIVER_GEM) {
  515.                 ret = drm_gem_init(dev);
  516.                 if (ret) {
  517.                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
  518.                         goto err_ctxbitmap;
  519.                 }
  520.         }
  521.  
  522.         return 0;
  523.  
  524. err_ctxbitmap:
  525. //   drm_lastclose(dev);
  526.         return ret;
  527. }
  528. EXPORT_SYMBOL(drm_fill_in_dev);
  529. /**
  530.  * Compute size order.  Returns the exponent of the smaller power of two which
  531.  * is greater or equal to given number.
  532.  *
  533.  * \param size size.
  534.  * \return order.
  535.  *
  536.  * \todo Can be made faster.
  537.  */
  538. int drm_order(unsigned long size)
  539. {
  540.     int order;
  541.     unsigned long tmp;
  542.  
  543.     for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
  544.  
  545.     if (size & (size - 1))
  546.         ++order;
  547.  
  548.     return order;
  549. }
  550.  
  551. int drm_sysfs_connector_add(struct drm_connector *connector)
  552. {
  553.     return 0;
  554. }
  555.  
  556. void drm_sysfs_connector_remove(struct drm_connector *connector)
  557. { }
  558.  
  559. void drm_sysfs_hotplug_event(struct drm_device *dev)
  560. {
  561.     DRM_DEBUG("generating hotplug event\n");
  562. }
  563.  
  564. u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
  565. {
  566.     u32 high = divisor >> 32;
  567.     u64 quot;
  568.  
  569.     if (high == 0) {
  570.         u32 rem32;
  571.         quot = div_u64_rem(dividend, divisor, &rem32);
  572.         *remainder = rem32;
  573.     } else {
  574.         int n = 1 + fls(high);
  575.         quot = div_u64(dividend >> n, divisor >> n);
  576.  
  577.         if (quot != 0)
  578.             quot--;
  579.  
  580.         *remainder = dividend - quot * divisor;
  581.         if (*remainder >= divisor) {
  582.             quot++;
  583.             *remainder -= divisor;
  584.         }
  585.     }
  586.  
  587.     return quot;
  588. }
  589.  
  590. u64 div64_u64(u64 dividend, u64 divisor)
  591. {
  592.         u32 high, d;
  593.  
  594.         high = divisor >> 32;
  595.         if (high) {
  596.                 unsigned int shift = fls(high);
  597.  
  598.                 d = divisor >> shift;
  599.                 dividend >>= shift;
  600.         } else
  601.                 d = divisor;
  602.  
  603.         return div_u64(dividend, d);
  604. }
  605.  
  606.