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 void 25 nvkm_chid_put(struct nvkm_chid *chid, int id, spinlock_t *data_lock) 26 { 27 if (id >= 0) { 28 spin_lock_irq(&chid->lock); 29 spin_lock(data_lock); 30 chid->data[id] = NULL; 31 spin_unlock(data_lock); 32 clear_bit(id, chid->used); 33 spin_unlock_irq(&chid->lock); 34 } 35 } 36 37 int 38 nvkm_chid_get(struct nvkm_chid *chid, void *data) 39 { 40 int id = -1, cid; 41 42 spin_lock_irq(&chid->lock); 43 cid = find_first_zero_bit(chid->used, chid->nr); 44 if (cid < chid->nr) { 45 set_bit(cid, chid->used); 46 chid->data[cid] = data; 47 id = cid; 48 } 49 spin_unlock_irq(&chid->lock); 50 return id; 51 } 52 53 static void 54 nvkm_chid_del(struct kref *kref) 55 { 56 struct nvkm_chid *chid = container_of(kref, typeof(*chid), kref); 57 58 nvkm_event_fini(&chid->event); 59 60 kvfree(chid->data); 61 kfree(chid); 62 } 63 64 void 65 nvkm_chid_unref(struct nvkm_chid **pchid) 66 { 67 struct nvkm_chid *chid = *pchid; 68 69 if (!chid) 70 return; 71 72 kref_put(&chid->kref, nvkm_chid_del); 73 *pchid = NULL; 74 } 75 76 struct nvkm_chid * 77 nvkm_chid_ref(struct nvkm_chid *chid) 78 { 79 if (chid) 80 kref_get(&chid->kref); 81 82 return chid; 83 } 84 85 int 86 nvkm_chid_new(const struct nvkm_event_func *func, struct nvkm_subdev *subdev, 87 int nr, int first, int count, struct nvkm_chid **pchid) 88 { 89 struct nvkm_chid *chid; 90 int id; 91 92 if (!(chid = *pchid = kzalloc(struct_size(chid, used, nr), GFP_KERNEL))) 93 return -ENOMEM; 94 95 kref_init(&chid->kref); 96 chid->nr = nr; 97 chid->mask = chid->nr - 1; 98 spin_lock_init(&chid->lock); 99 100 if (!(chid->data = kvzalloc(sizeof(*chid->data) * nr, GFP_KERNEL))) { 101 nvkm_chid_unref(pchid); 102 return -ENOMEM; 103 } 104 105 for (id = 0; id < first; id++) 106 __set_bit(id, chid->used); 107 for (id = first + count; id < nr; id++) 108 __set_bit(id, chid->used); 109 110 return nvkm_event_init(func, subdev, 1, nr, &chid->event); 111 } 112