Merge remote-tracking branch 'airlied/drm-next' into topic/vblank-rework
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_auth.c
1 /**
2  * \file drm_auth.c
3  * IOCTLs for authentication
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Created: Tue Feb  2 08:37:54 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
36 #include <drm/drmP.h>
37
38 struct drm_magic_entry {
39         struct list_head head;
40         struct drm_hash_item hash_item;
41         struct drm_file *priv;
42 };
43
44 /**
45  * Find the file with the given magic number.
46  *
47  * \param dev DRM device.
48  * \param magic magic number.
49  *
50  * Searches in drm_device::magiclist within all files with the same hash key
51  * the one with matching magic number, while holding the drm_device::struct_mutex
52  * lock.
53  */
54 static struct drm_file *drm_find_file(struct drm_master *master, drm_magic_t magic)
55 {
56         struct drm_file *retval = NULL;
57         struct drm_magic_entry *pt;
58         struct drm_hash_item *hash;
59         struct drm_device *dev = master->minor->dev;
60
61         mutex_lock(&dev->struct_mutex);
62         if (!drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
63                 pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
64                 retval = pt->priv;
65         }
66         mutex_unlock(&dev->struct_mutex);
67         return retval;
68 }
69
70 /**
71  * Adds a magic number.
72  *
73  * \param dev DRM device.
74  * \param priv file private data.
75  * \param magic magic number.
76  *
77  * Creates a drm_magic_entry structure and appends to the linked list
78  * associated the magic number hash key in drm_device::magiclist, while holding
79  * the drm_device::struct_mutex lock.
80  */
81 static int drm_add_magic(struct drm_master *master, struct drm_file *priv,
82                          drm_magic_t magic)
83 {
84         struct drm_magic_entry *entry;
85         struct drm_device *dev = master->minor->dev;
86         DRM_DEBUG("%d\n", magic);
87
88         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
89         if (!entry)
90                 return -ENOMEM;
91         entry->priv = priv;
92         entry->hash_item.key = (unsigned long)magic;
93         mutex_lock(&dev->struct_mutex);
94         drm_ht_insert_item(&master->magiclist, &entry->hash_item);
95         list_add_tail(&entry->head, &master->magicfree);
96         mutex_unlock(&dev->struct_mutex);
97
98         return 0;
99 }
100
101 /**
102  * Remove a magic number.
103  *
104  * \param dev DRM device.
105  * \param magic magic number.
106  *
107  * Searches and unlinks the entry in drm_device::magiclist with the magic
108  * number hash key, while holding the drm_device::struct_mutex lock.
109  */
110 int drm_remove_magic(struct drm_master *master, drm_magic_t magic)
111 {
112         struct drm_magic_entry *pt;
113         struct drm_hash_item *hash;
114         struct drm_device *dev = master->minor->dev;
115
116         DRM_DEBUG("%d\n", magic);
117
118         mutex_lock(&dev->struct_mutex);
119         if (drm_ht_find_item(&master->magiclist, (unsigned long)magic, &hash)) {
120                 mutex_unlock(&dev->struct_mutex);
121                 return -EINVAL;
122         }
123         pt = drm_hash_entry(hash, struct drm_magic_entry, hash_item);
124         drm_ht_remove_item(&master->magiclist, hash);
125         list_del(&pt->head);
126         mutex_unlock(&dev->struct_mutex);
127
128         kfree(pt);
129
130         return 0;
131 }
132
133 /**
134  * Get a unique magic number (ioctl).
135  *
136  * \param inode device inode.
137  * \param file_priv DRM file private.
138  * \param cmd command.
139  * \param arg pointer to a resulting drm_auth structure.
140  * \return zero on success, or a negative number on failure.
141  *
142  * If there is a magic number in drm_file::magic then use it, otherwise
143  * searches an unique non-zero magic number and add it associating it with \p
144  * file_priv.
145  * This ioctl needs protection by the drm_global_mutex, which protects
146  * struct drm_file::magic and struct drm_magic_entry::priv.
147  */
148 int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
149 {
150         static drm_magic_t sequence = 0;
151         static DEFINE_SPINLOCK(lock);
152         struct drm_auth *auth = data;
153
154         /* Find unique magic */
155         if (file_priv->magic) {
156                 auth->magic = file_priv->magic;
157         } else {
158                 do {
159                         spin_lock(&lock);
160                         if (!sequence)
161                                 ++sequence;     /* reserve 0 */
162                         auth->magic = sequence++;
163                         spin_unlock(&lock);
164                 } while (drm_find_file(file_priv->master, auth->magic));
165                 file_priv->magic = auth->magic;
166                 drm_add_magic(file_priv->master, file_priv, auth->magic);
167         }
168
169         DRM_DEBUG("%u\n", auth->magic);
170
171         return 0;
172 }
173
174 /**
175  * Authenticate with a magic.
176  *
177  * \param inode device inode.
178  * \param file_priv DRM file private.
179  * \param cmd command.
180  * \param arg pointer to a drm_auth structure.
181  * \return zero if authentication successed, or a negative number otherwise.
182  *
183  * Checks if \p file_priv is associated with the magic number passed in \arg.
184  * This ioctl needs protection by the drm_global_mutex, which protects
185  * struct drm_file::magic and struct drm_magic_entry::priv.
186  */
187 int drm_authmagic(struct drm_device *dev, void *data,
188                   struct drm_file *file_priv)
189 {
190         struct drm_auth *auth = data;
191         struct drm_file *file;
192
193         DRM_DEBUG("%u\n", auth->magic);
194         if ((file = drm_find_file(file_priv->master, auth->magic))) {
195                 file->authenticated = 1;
196                 drm_remove_magic(file_priv->master, auth->magic);
197                 return 0;
198         }
199         return -EINVAL;
200 }