1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
10 #include <linux/list.h>
11 #include <linux/slab.h>
15 struct spu_gang *alloc_spu_gang(void)
17 struct spu_gang *gang;
19 gang = kzalloc(sizeof *gang, GFP_KERNEL);
23 kref_init(&gang->kref);
24 mutex_init(&gang->mutex);
25 mutex_init(&gang->aff_mutex);
26 INIT_LIST_HEAD(&gang->list);
27 INIT_LIST_HEAD(&gang->aff_list_head);
33 static void destroy_spu_gang(struct kref *kref)
35 struct spu_gang *gang;
36 gang = container_of(kref, struct spu_gang, kref);
37 WARN_ON(gang->contexts || !list_empty(&gang->list));
41 struct spu_gang *get_spu_gang(struct spu_gang *gang)
43 kref_get(&gang->kref);
47 int put_spu_gang(struct spu_gang *gang)
49 return kref_put(&gang->kref, &destroy_spu_gang);
52 void spu_gang_add_ctx(struct spu_gang *gang, struct spu_context *ctx)
54 mutex_lock(&gang->mutex);
55 ctx->gang = get_spu_gang(gang);
56 list_add(&ctx->gang_list, &gang->list);
58 mutex_unlock(&gang->mutex);
61 void spu_gang_remove_ctx(struct spu_gang *gang, struct spu_context *ctx)
63 mutex_lock(&gang->mutex);
64 WARN_ON(ctx->gang != gang);
65 if (!list_empty(&ctx->aff_list)) {
66 list_del_init(&ctx->aff_list);
67 gang->aff_flags &= ~AFF_OFFSETS_SET;
69 list_del_init(&ctx->gang_list);
71 mutex_unlock(&gang->mutex);