xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/engine/fifo/chid.c (revision 6de125383a5cce5f0d9235a6d3a9ae83dc5d299e)
1 /*
2  * Copyright 2020 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #include "chid.h"
23 
24 static void
25 nvkm_chid_del(struct kref *kref)
26 {
27 	struct nvkm_chid *chid = container_of(kref, typeof(*chid), kref);
28 
29 	nvkm_event_fini(&chid->event);
30 
31 	kvfree(chid->data);
32 	kfree(chid);
33 }
34 
35 void
36 nvkm_chid_unref(struct nvkm_chid **pchid)
37 {
38 	struct nvkm_chid *chid = *pchid;
39 
40 	if (!chid)
41 		return;
42 
43 	kref_put(&chid->kref, nvkm_chid_del);
44 	*pchid = NULL;
45 }
46 
47 struct nvkm_chid *
48 nvkm_chid_ref(struct nvkm_chid *chid)
49 {
50 	if (chid)
51 		kref_get(&chid->kref);
52 
53 	return chid;
54 }
55 
56 int
57 nvkm_chid_new(const struct nvkm_event_func *func, struct nvkm_subdev *subdev,
58 	      int nr, int first, int count, struct nvkm_chid **pchid)
59 {
60 	struct nvkm_chid *chid;
61 	int id;
62 
63 	if (!(chid = *pchid = kzalloc(struct_size(chid, used, nr), GFP_KERNEL)))
64 		return -ENOMEM;
65 
66 	kref_init(&chid->kref);
67 	chid->nr = nr;
68 	chid->mask = chid->nr - 1;
69 	spin_lock_init(&chid->lock);
70 
71 	if (!(chid->data = kvzalloc(sizeof(*chid->data) * nr, GFP_KERNEL))) {
72 		nvkm_chid_unref(pchid);
73 		return -ENOMEM;
74 	}
75 
76 	for (id = 0; id < first; id++)
77 		__set_bit(id, chid->used);
78 	for (id = first + count; id < nr; id++)
79 		__set_bit(id, chid->used);
80 
81 	return nvkm_event_init(func, subdev, 1, nr, &chid->event);
82 }
83