ARM: dts: add CPU nodes for Exynos4 SoCs
[cascardo/linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_cmdbuf_res.c
1 /**************************************************************************
2  *
3  * Copyright © 2014 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #include "vmwgfx_drv.h"
29
30 #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
31
32 enum vmw_cmdbuf_res_state {
33         VMW_CMDBUF_RES_COMMITED,
34         VMW_CMDBUF_RES_ADD,
35         VMW_CMDBUF_RES_DEL
36 };
37
38 /**
39  * struct vmw_cmdbuf_res - Command buffer managed resource entry.
40  *
41  * @res: Refcounted pointer to a struct vmw_resource.
42  * @hash: Hash entry for the manager hash table.
43  * @head: List head used either by the staging list or the manager list
44  * of commited resources.
45  * @state: Staging state of this resource entry.
46  * @man: Pointer to a resource manager for this entry.
47  */
48 struct vmw_cmdbuf_res {
49         struct vmw_resource *res;
50         struct drm_hash_item hash;
51         struct list_head head;
52         enum vmw_cmdbuf_res_state state;
53         struct vmw_cmdbuf_res_manager *man;
54 };
55
56 /**
57  * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
58  *
59  * @resources: Hash table containing staged and commited command buffer
60  * resources
61  * @list: List of commited command buffer resources.
62  * @dev_priv: Pointer to a device private structure.
63  *
64  * @resources and @list are protected by the cmdbuf mutex for now.
65  */
66 struct vmw_cmdbuf_res_manager {
67         struct drm_open_hash resources;
68         struct list_head list;
69         struct vmw_private *dev_priv;
70 };
71
72
73 /**
74  * vmw_cmdbuf_res_lookup - Look up a command buffer resource
75  *
76  * @man: Pointer to the command buffer resource manager
77  * @resource_type: The resource type, that combined with the user key
78  * identifies the resource.
79  * @user_key: The user key.
80  *
81  * Returns a valid refcounted struct vmw_resource pointer on success,
82  * an error pointer on failure.
83  */
84 struct vmw_resource *
85 vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
86                       enum vmw_cmdbuf_res_type res_type,
87                       u32 user_key)
88 {
89         struct drm_hash_item *hash;
90         int ret;
91         unsigned long key = user_key | (res_type << 24);
92
93         ret = drm_ht_find_item(&man->resources, key, &hash);
94         if (unlikely(ret != 0))
95                 return ERR_PTR(ret);
96
97         return vmw_resource_reference
98                 (drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res);
99 }
100
101 /**
102  * vmw_cmdbuf_res_free - Free a command buffer resource.
103  *
104  * @man: Pointer to the command buffer resource manager
105  * @entry: Pointer to a struct vmw_cmdbuf_res.
106  *
107  * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
108  * struct vmw_resource.
109  */
110 static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
111                                 struct vmw_cmdbuf_res *entry)
112 {
113         list_del(&entry->head);
114         WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
115         vmw_resource_unreference(&entry->res);
116         kfree(entry);
117 }
118
119 /**
120  * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
121  *
122  * @list: Caller's list of command buffer resource actions.
123  *
124  * This function commits a list of command buffer resource
125  * additions or removals.
126  * It is typically called when the execbuf ioctl call triggering these
127  * actions has commited the fifo contents to the device.
128  */
129 void vmw_cmdbuf_res_commit(struct list_head *list)
130 {
131         struct vmw_cmdbuf_res *entry, *next;
132
133         list_for_each_entry_safe(entry, next, list, head) {
134                 list_del(&entry->head);
135                 switch (entry->state) {
136                 case VMW_CMDBUF_RES_ADD:
137                         entry->state = VMW_CMDBUF_RES_COMMITED;
138                         list_add_tail(&entry->head, &entry->man->list);
139                         break;
140                 case VMW_CMDBUF_RES_DEL:
141                         vmw_resource_unreference(&entry->res);
142                         kfree(entry);
143                         break;
144                 default:
145                         BUG();
146                         break;
147                 }
148         }
149 }
150
151 /**
152  * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
153  *
154  * @man: Pointer to the command buffer resource manager
155  * @list: Caller's list of command buffer resource action
156  *
157  * This function reverts a list of command buffer resource
158  * additions or removals.
159  * It is typically called when the execbuf ioctl call triggering these
160  * actions failed for some reason, and the command stream was never
161  * submitted.
162  */
163 void vmw_cmdbuf_res_revert(struct list_head *list)
164 {
165         struct vmw_cmdbuf_res *entry, *next;
166         int ret;
167
168         list_for_each_entry_safe(entry, next, list, head) {
169                 switch (entry->state) {
170                 case VMW_CMDBUF_RES_ADD:
171                         vmw_cmdbuf_res_free(entry->man, entry);
172                         break;
173                 case VMW_CMDBUF_RES_DEL:
174                         ret = drm_ht_insert_item(&entry->man->resources,
175                                                  &entry->hash);
176                         list_del(&entry->head);
177                         list_add_tail(&entry->head, &entry->man->list);
178                         entry->state = VMW_CMDBUF_RES_COMMITED;
179                         break;
180                 default:
181                         BUG();
182                         break;
183                 }
184         }
185 }
186
187 /**
188  * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
189  *
190  * @man: Pointer to the command buffer resource manager.
191  * @res_type: The resource type.
192  * @user_key: The user-space id of the resource.
193  * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
194  * @list: The staging list.
195  *
196  * This function allocates a struct vmw_cmdbuf_res entry and adds the
197  * resource to the hash table of the manager identified by @man. The
198  * entry is then put on the staging list identified by @list.
199  */
200 int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
201                        enum vmw_cmdbuf_res_type res_type,
202                        u32 user_key,
203                        struct vmw_resource *res,
204                        struct list_head *list)
205 {
206         struct vmw_cmdbuf_res *cres;
207         int ret;
208
209         cres = kzalloc(sizeof(*cres), GFP_KERNEL);
210         if (unlikely(cres == NULL))
211                 return -ENOMEM;
212
213         cres->hash.key = user_key | (res_type << 24);
214         ret = drm_ht_insert_item(&man->resources, &cres->hash);
215         if (unlikely(ret != 0))
216                 goto out_invalid_key;
217
218         cres->state = VMW_CMDBUF_RES_ADD;
219         cres->res = vmw_resource_reference(res);
220         cres->man = man;
221         list_add_tail(&cres->head, list);
222
223 out_invalid_key:
224         return ret;
225 }
226
227 /**
228  * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
229  *
230  * @man: Pointer to the command buffer resource manager.
231  * @res_type: The resource type.
232  * @user_key: The user-space id of the resource.
233  * @list: The staging list.
234  *
235  * This function looks up the struct vmw_cmdbuf_res entry from the manager
236  * hash table and, if it exists, removes it. Depending on its current staging
237  * state it then either removes the entry from the staging list or adds it
238  * to it with a staging state of removal.
239  */
240 int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
241                           enum vmw_cmdbuf_res_type res_type,
242                           u32 user_key,
243                           struct list_head *list)
244 {
245         struct vmw_cmdbuf_res *entry;
246         struct drm_hash_item *hash;
247         int ret;
248
249         ret = drm_ht_find_item(&man->resources, user_key, &hash);
250         if (likely(ret != 0))
251                 return -EINVAL;
252
253         entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
254
255         switch (entry->state) {
256         case VMW_CMDBUF_RES_ADD:
257                 vmw_cmdbuf_res_free(man, entry);
258                 break;
259         case VMW_CMDBUF_RES_COMMITED:
260                 (void) drm_ht_remove_item(&man->resources, &entry->hash);
261                 list_del(&entry->head);
262                 entry->state = VMW_CMDBUF_RES_DEL;
263                 list_add_tail(&entry->head, list);
264                 break;
265         default:
266                 BUG();
267                 break;
268         }
269
270         return 0;
271 }
272
273 /**
274  * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
275  * manager.
276  *
277  * @dev_priv: Pointer to a struct vmw_private
278  *
279  * Allocates and initializes a command buffer managed resource manager. Returns
280  * an error pointer on failure.
281  */
282 struct vmw_cmdbuf_res_manager *
283 vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
284 {
285         struct vmw_cmdbuf_res_manager *man;
286         int ret;
287
288         man = kzalloc(sizeof(*man), GFP_KERNEL);
289         if (man == NULL)
290                 return ERR_PTR(-ENOMEM);
291
292         man->dev_priv = dev_priv;
293         INIT_LIST_HEAD(&man->list);
294         ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
295         if (ret == 0)
296                 return man;
297
298         kfree(man);
299         return ERR_PTR(ret);
300 }
301
302 /**
303  * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
304  * manager.
305  *
306  * @man: Pointer to the  manager to destroy.
307  *
308  * This function destroys a command buffer managed resource manager and
309  * unreferences / frees all command buffer managed resources and -entries
310  * associated with it.
311  */
312 void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
313 {
314         struct vmw_cmdbuf_res *entry, *next;
315
316         list_for_each_entry_safe(entry, next, &man->list, head)
317                 vmw_cmdbuf_res_free(man, entry);
318
319         kfree(man);
320 }
321
322 /**
323  *
324  * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
325  * resource manager
326  *
327  * Returns the approximate allocation size of a command buffer managed
328  * resource manager.
329  */
330 size_t vmw_cmdbuf_res_man_size(void)
331 {
332         static size_t res_man_size;
333
334         if (unlikely(res_man_size == 0))
335                 res_man_size =
336                         ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
337                         ttm_round_pot(sizeof(struct hlist_head) <<
338                                       VMW_CMDBUF_RES_MAN_HT_ORDER);
339
340         return res_man_size;
341 }