mmu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. static const struct nvif_mclass mems[] = {
  37. { NVIF_CLASS_MEM_GF100, -1 },
  38. { NVIF_CLASS_MEM_NV50 , -1 },
  39. { NVIF_CLASS_MEM_NV04 , -1 },
  40. {}
  41. };
  42. struct nvif_mmu_v0 args;
  43. int ret, i;
  44. args.version = 0;
  45. mmu->heap = NULL;
  46. mmu->type = NULL;
  47. mmu->kind = NULL;
  48. ret = nvif_object_init(parent, 0, oclass, &args, sizeof(args),
  49. &mmu->object);
  50. if (ret)
  51. goto done;
  52. mmu->dmabits = args.dmabits;
  53. mmu->heap_nr = args.heap_nr;
  54. mmu->type_nr = args.type_nr;
  55. mmu->kind_nr = args.kind_nr;
  56. ret = nvif_mclass(&mmu->object, mems);
  57. if (ret < 0)
  58. goto done;
  59. mmu->mem = mems[ret].oclass;
  60. mmu->heap = kmalloc_array(mmu->heap_nr, sizeof(*mmu->heap),
  61. GFP_KERNEL);
  62. mmu->type = kmalloc_array(mmu->type_nr, sizeof(*mmu->type),
  63. GFP_KERNEL);
  64. if (ret = -ENOMEM, !mmu->heap || !mmu->type)
  65. goto done;
  66. mmu->kind = kmalloc_array(mmu->kind_nr, sizeof(*mmu->kind),
  67. GFP_KERNEL);
  68. if (!mmu->kind && mmu->kind_nr)
  69. goto done;
  70. for (i = 0; i < mmu->heap_nr; i++) {
  71. struct nvif_mmu_heap_v0 args = { .index = i };
  72. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP,
  73. &args, sizeof(args));
  74. if (ret)
  75. goto done;
  76. mmu->heap[i].size = args.size;
  77. }
  78. for (i = 0; i < mmu->type_nr; i++) {
  79. struct nvif_mmu_type_v0 args = { .index = i };
  80. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE,
  81. &args, sizeof(args));
  82. if (ret)
  83. goto done;
  84. mmu->type[i].type = 0;
  85. if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM;
  86. if (args.host) mmu->type[i].type |= NVIF_MEM_HOST;
  87. if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP;
  88. if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP;
  89. if (args.kind ) mmu->type[i].type |= NVIF_MEM_KIND;
  90. if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE;
  91. if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT;
  92. if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED;
  93. mmu->type[i].heap = args.heap;
  94. }
  95. if (mmu->kind_nr) {
  96. struct nvif_mmu_kind_v0 *kind;
  97. u32 argc = sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;
  98. if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
  99. goto done;
  100. kind->version = 0;
  101. kind->count = mmu->kind_nr;
  102. ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND,
  103. kind, argc);
  104. if (ret == 0)
  105. memcpy(mmu->kind, kind->data, kind->count);
  106. kfree(kind);
  107. }
  108. done:
  109. if (ret)
  110. nvif_mmu_fini(mmu);
  111. return ret;
  112. }