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