mmu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/mmu.h>
  23. #include <nvif/class.h>
  24. #include <nvif/if0008.h>
  25. void
  26. nvif_mmu_fini(struct nvif_mmu *mmu)
  27. {
  28. kfree(mmu->kind);
  29. kfree(mmu->type);
  30. kfree(mmu->heap);
  31. nvif_object_fini(&mmu->object);
  32. }
  33. int
  34. nvif_mmu_init(struct nvif_object *parent, s32 oclass, struct nvif_mmu *mmu)
  35. {
  36. struct nvif_mmu_v0 args;
  37. int ret, i;
  38. args.version = 0;
  39. mmu->heap = NULL;
  40. mmu->type = NULL;
  41. mmu->kind = NULL;
  42. ret = nvif_object_init(parent, 0, oclass, &args, sizeof(args),
  43. &mmu->object);
  44. if (ret)
  45. goto done;
  46. mmu->dmabits = args.dmabits;
  47. mmu->heap_nr = args.heap_nr;
  48. mmu->type_nr = args.type_nr;
  49. mmu->kind_nr = args.kind_nr;
  50. mmu->heap = kmalloc(sizeof(*mmu->heap) * mmu->heap_nr, GFP_KERNEL);
  51. mmu->type = kmalloc(sizeof(*mmu->type) * mmu->type_nr, GFP_KERNEL);
  52. if (ret = -ENOMEM, !mmu->heap || !mmu->type)
  53. goto done;
  54. mmu->kind = kmalloc(sizeof(*mmu->kind) * mmu->kind_nr, GFP_KERNEL);
  55. if (!mmu->kind && mmu->kind_nr)
  56. goto done;
  57. for (i = 0; i < mmu->heap_nr; i++) {
  58. struct nvif_mmu_heap_v0 args = { .index = i };
  59. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP,
  60. &args, sizeof(args));
  61. if (ret)
  62. goto done;
  63. mmu->heap[i].size = args.size;
  64. }
  65. for (i = 0; i < mmu->type_nr; i++) {
  66. struct nvif_mmu_type_v0 args = { .index = i };
  67. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE,
  68. &args, sizeof(args));
  69. if (ret)
  70. goto done;
  71. mmu->type[i].type = 0;
  72. if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM;
  73. if (args.host) mmu->type[i].type |= NVIF_MEM_HOST;
  74. if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP;
  75. if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP;
  76. if (args.kind ) mmu->type[i].type |= NVIF_MEM_KIND;
  77. if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE;
  78. if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT;
  79. if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED;
  80. mmu->type[i].heap = args.heap;
  81. }
  82. if (mmu->kind_nr) {
  83. struct nvif_mmu_kind_v0 *kind;
  84. u32 argc = sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;
  85. if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
  86. goto done;
  87. kind->version = 0;
  88. kind->count = mmu->kind_nr;
  89. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND,
  90. kind, argc);
  91. if (ret == 0)
  92. memcpy(mmu->kind, kind->data, kind->count);
  93. kfree(kind);
  94. }
  95. done:
  96. if (ret)
  97. nvif_mmu_fini(mmu);
  98. return ret;
  99. }