mem.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2017 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 <nvif/mem.h>
  23. #include <nvif/client.h>
  24. #include <nvif/if000a.h>
  25. void
  26. nvif_mem_fini(struct nvif_mem *mem)
  27. {
  28. nvif_object_fini(&mem->object);
  29. }
  30. int
  31. nvif_mem_init_type(struct nvif_mmu *mmu, s32 oclass, int type, u8 page,
  32. u64 size, void *argv, u32 argc, struct nvif_mem *mem)
  33. {
  34. struct nvif_mem_v0 *args;
  35. u8 stack[128];
  36. int ret;
  37. mem->object.client = NULL;
  38. if (type < 0)
  39. return -EINVAL;
  40. if (sizeof(*args) + argc > sizeof(stack)) {
  41. if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
  42. return -ENOMEM;
  43. } else {
  44. args = (void *)stack;
  45. }
  46. args->version = 0;
  47. args->type = type;
  48. args->page = page;
  49. args->size = size;
  50. memcpy(args->data, argv, argc);
  51. ret = nvif_object_init(&mmu->object, 0, oclass, args,
  52. sizeof(*args) + argc, &mem->object);
  53. if (ret == 0) {
  54. mem->type = mmu->type[type].type;
  55. mem->page = args->page;
  56. mem->addr = args->addr;
  57. mem->size = args->size;
  58. }
  59. if (args != (void *)stack)
  60. kfree(args);
  61. return ret;
  62. }
  63. int
  64. nvif_mem_init(struct nvif_mmu *mmu, s32 oclass, u8 type, u8 page,
  65. u64 size, void *argv, u32 argc, struct nvif_mem *mem)
  66. {
  67. int ret = -EINVAL, i;
  68. mem->object.client = NULL;
  69. for (i = 0; ret && i < mmu->type_nr; i++) {
  70. if ((mmu->type[i].type & type) == type) {
  71. ret = nvif_mem_init_type(mmu, oclass, i, page, size,
  72. argv, argc, mem);
  73. }
  74. }
  75. return ret;
  76. }