context.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SPU file system -- SPU context management
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  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)
  11. * any later version.
  12. *
  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.
  17. *
  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.
  21. */
  22. #include <linux/slab.h>
  23. #include <asm/spu.h>
  24. #include "spufs.h"
  25. struct spu_context *alloc_spu_context(void)
  26. {
  27. struct spu_context *ctx;
  28. ctx = kmalloc(sizeof *ctx, GFP_KERNEL);
  29. if (!ctx)
  30. goto out;
  31. ctx->spu = spu_alloc();
  32. if (!ctx->spu)
  33. goto out_free;
  34. init_rwsem(&ctx->backing_sema);
  35. spin_lock_init(&ctx->mmio_lock);
  36. kref_init(&ctx->kref);
  37. goto out;
  38. out_free:
  39. kfree(ctx);
  40. ctx = NULL;
  41. out:
  42. return ctx;
  43. }
  44. void destroy_spu_context(struct kref *kref)
  45. {
  46. struct spu_context *ctx;
  47. ctx = container_of(kref, struct spu_context, kref);
  48. if (ctx->spu)
  49. spu_free(ctx->spu);
  50. kfree(ctx);
  51. }
  52. struct spu_context * get_spu_context(struct spu_context *ctx)
  53. {
  54. kref_get(&ctx->kref);
  55. return ctx;
  56. }
  57. int put_spu_context(struct spu_context *ctx)
  58. {
  59. return kref_put(&ctx->kref, &destroy_spu_context);
  60. }