4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/list.h>
24 #include <linux/slab.h>
28 struct spu_gang
*alloc_spu_gang(void)
30 struct spu_gang
*gang
;
32 gang
= kzalloc(sizeof *gang
, GFP_KERNEL
);
36 kref_init(&gang
->kref
);
37 mutex_init(&gang
->mutex
);
38 mutex_init(&gang
->aff_mutex
);
39 INIT_LIST_HEAD(&gang
->list
);
40 INIT_LIST_HEAD(&gang
->aff_list_head
);
46 static void destroy_spu_gang(struct kref
*kref
)
48 struct spu_gang
*gang
;
49 gang
= container_of(kref
, struct spu_gang
, kref
);
50 WARN_ON(gang
->contexts
|| !list_empty(&gang
->list
));
54 struct spu_gang
*get_spu_gang(struct spu_gang
*gang
)
56 kref_get(&gang
->kref
);
60 int put_spu_gang(struct spu_gang
*gang
)
62 return kref_put(&gang
->kref
, &destroy_spu_gang
);
65 void spu_gang_add_ctx(struct spu_gang
*gang
, struct spu_context
*ctx
)
67 mutex_lock(&gang
->mutex
);
68 ctx
->gang
= get_spu_gang(gang
);
69 list_add(&ctx
->gang_list
, &gang
->list
);
71 mutex_unlock(&gang
->mutex
);
74 void spu_gang_remove_ctx(struct spu_gang
*gang
, struct spu_context
*ctx
)
76 mutex_lock(&gang
->mutex
);
77 WARN_ON(ctx
->gang
!= gang
);
78 if (!list_empty(&ctx
->aff_list
))
79 list_del_init(&ctx
->aff_list
);
80 list_del_init(&ctx
->gang_list
);
82 mutex_unlock(&gang
->mutex
);