mem.c 2.7 KB

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