1 /* $NetBSD: nouveau_nvif_mmu.c,v 1.2 2021/12/18 23:45:33 riastradh Exp $ */ 2 3 /* 4 * Copyright 2017 Red Hat Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #include <sys/cdefs.h> 25 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvif_mmu.c,v 1.2 2021/12/18 23:45:33 riastradh Exp $"); 26 27 #include <nvif/mmu.h> 28 29 #include <nvif/class.h> 30 #include <nvif/if0008.h> 31 32 void 33 nvif_mmu_fini(struct nvif_mmu *mmu) 34 { 35 kfree(mmu->kind); 36 kfree(mmu->type); 37 kfree(mmu->heap); 38 nvif_object_fini(&mmu->object); 39 } 40 41 int 42 nvif_mmu_init(struct nvif_object *parent, s32 oclass, struct nvif_mmu *mmu) 43 { 44 static const struct nvif_mclass mems[] = { 45 { NVIF_CLASS_MEM_GF100, -1 }, 46 { NVIF_CLASS_MEM_NV50 , -1 }, 47 { NVIF_CLASS_MEM_NV04 , -1 }, 48 {} 49 }; 50 struct nvif_mmu_v0 args; 51 int ret, i; 52 53 args.version = 0; 54 mmu->heap = NULL; 55 mmu->type = NULL; 56 mmu->kind = NULL; 57 58 ret = nvif_object_init(parent, 0, oclass, &args, sizeof(args), 59 &mmu->object); 60 if (ret) 61 goto done; 62 63 mmu->dmabits = args.dmabits; 64 mmu->heap_nr = args.heap_nr; 65 mmu->type_nr = args.type_nr; 66 mmu->kind_nr = args.kind_nr; 67 68 ret = nvif_mclass(&mmu->object, mems); 69 if (ret < 0) 70 goto done; 71 mmu->mem = mems[ret].oclass; 72 73 mmu->heap = kmalloc_array(mmu->heap_nr, sizeof(*mmu->heap), 74 GFP_KERNEL); 75 mmu->type = kmalloc_array(mmu->type_nr, sizeof(*mmu->type), 76 GFP_KERNEL); 77 if (ret = -ENOMEM, !mmu->heap || !mmu->type) 78 goto done; 79 80 mmu->kind = kmalloc_array(mmu->kind_nr, sizeof(*mmu->kind), 81 GFP_KERNEL); 82 if (!mmu->kind && mmu->kind_nr) 83 goto done; 84 85 for (i = 0; i < mmu->heap_nr; i++) { 86 struct nvif_mmu_heap_v0 args = { .index = i }; 87 88 ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP, 89 &args, sizeof(args)); 90 if (ret) 91 goto done; 92 93 mmu->heap[i].size = args.size; 94 } 95 96 for (i = 0; i < mmu->type_nr; i++) { 97 struct nvif_mmu_type_v0 args = { .index = i }; 98 99 ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE, 100 &args, sizeof(args)); 101 if (ret) 102 goto done; 103 104 mmu->type[i].type = 0; 105 if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM; 106 if (args.host) mmu->type[i].type |= NVIF_MEM_HOST; 107 if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP; 108 if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP; 109 if (args.kind ) mmu->type[i].type |= NVIF_MEM_KIND; 110 if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE; 111 if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT; 112 if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED; 113 mmu->type[i].heap = args.heap; 114 } 115 116 if (mmu->kind_nr) { 117 struct nvif_mmu_kind_v0 *kind; 118 size_t argc = struct_size(kind, data, mmu->kind_nr); 119 120 if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL))) 121 goto done; 122 kind->version = 0; 123 kind->count = mmu->kind_nr; 124 125 ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND, 126 kind, argc); 127 if (ret == 0) 128 memcpy(mmu->kind, kind->data, kind->count); 129 mmu->kind_inv = kind->kind_inv; 130 kfree(kind); 131 } 132 133 done: 134 if (ret) 135 nvif_mmu_fini(mmu); 136 return ret; 137 } 138