18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2017 Red Hat Inc. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ci#include <nvif/mmu.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include <nvif/class.h> 258c2ecf20Sopenharmony_ci#include <nvif/if0008.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_civoid 288c2ecf20Sopenharmony_cinvif_mmu_dtor(struct nvif_mmu *mmu) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci kfree(mmu->kind); 318c2ecf20Sopenharmony_ci kfree(mmu->type); 328c2ecf20Sopenharmony_ci kfree(mmu->heap); 338c2ecf20Sopenharmony_ci nvif_object_dtor(&mmu->object); 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ciint 378c2ecf20Sopenharmony_cinvif_mmu_ctor(struct nvif_object *parent, const char *name, s32 oclass, 388c2ecf20Sopenharmony_ci struct nvif_mmu *mmu) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci static const struct nvif_mclass mems[] = { 418c2ecf20Sopenharmony_ci { NVIF_CLASS_MEM_GF100, -1 }, 428c2ecf20Sopenharmony_ci { NVIF_CLASS_MEM_NV50 , -1 }, 438c2ecf20Sopenharmony_ci { NVIF_CLASS_MEM_NV04 , -1 }, 448c2ecf20Sopenharmony_ci {} 458c2ecf20Sopenharmony_ci }; 468c2ecf20Sopenharmony_ci struct nvif_mmu_v0 args; 478c2ecf20Sopenharmony_ci int ret, i; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci args.version = 0; 508c2ecf20Sopenharmony_ci mmu->heap = NULL; 518c2ecf20Sopenharmony_ci mmu->type = NULL; 528c2ecf20Sopenharmony_ci mmu->kind = NULL; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci ret = nvif_object_ctor(parent, name ? name : "nvifMmu", 0, oclass, 558c2ecf20Sopenharmony_ci &args, sizeof(args), &mmu->object); 568c2ecf20Sopenharmony_ci if (ret) 578c2ecf20Sopenharmony_ci goto done; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci mmu->dmabits = args.dmabits; 608c2ecf20Sopenharmony_ci mmu->heap_nr = args.heap_nr; 618c2ecf20Sopenharmony_ci mmu->type_nr = args.type_nr; 628c2ecf20Sopenharmony_ci mmu->kind_nr = args.kind_nr; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci ret = nvif_mclass(&mmu->object, mems); 658c2ecf20Sopenharmony_ci if (ret < 0) 668c2ecf20Sopenharmony_ci goto done; 678c2ecf20Sopenharmony_ci mmu->mem = mems[ret].oclass; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci mmu->heap = kmalloc_array(mmu->heap_nr, sizeof(*mmu->heap), 708c2ecf20Sopenharmony_ci GFP_KERNEL); 718c2ecf20Sopenharmony_ci mmu->type = kmalloc_array(mmu->type_nr, sizeof(*mmu->type), 728c2ecf20Sopenharmony_ci GFP_KERNEL); 738c2ecf20Sopenharmony_ci if (ret = -ENOMEM, !mmu->heap || !mmu->type) 748c2ecf20Sopenharmony_ci goto done; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci mmu->kind = kmalloc_array(mmu->kind_nr, sizeof(*mmu->kind), 778c2ecf20Sopenharmony_ci GFP_KERNEL); 788c2ecf20Sopenharmony_ci if (!mmu->kind && mmu->kind_nr) 798c2ecf20Sopenharmony_ci goto done; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci for (i = 0; i < mmu->heap_nr; i++) { 828c2ecf20Sopenharmony_ci struct nvif_mmu_heap_v0 args = { .index = i }; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP, 858c2ecf20Sopenharmony_ci &args, sizeof(args)); 868c2ecf20Sopenharmony_ci if (ret) 878c2ecf20Sopenharmony_ci goto done; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci mmu->heap[i].size = args.size; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci for (i = 0; i < mmu->type_nr; i++) { 938c2ecf20Sopenharmony_ci struct nvif_mmu_type_v0 args = { .index = i }; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE, 968c2ecf20Sopenharmony_ci &args, sizeof(args)); 978c2ecf20Sopenharmony_ci if (ret) 988c2ecf20Sopenharmony_ci goto done; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci mmu->type[i].type = 0; 1018c2ecf20Sopenharmony_ci if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM; 1028c2ecf20Sopenharmony_ci if (args.host) mmu->type[i].type |= NVIF_MEM_HOST; 1038c2ecf20Sopenharmony_ci if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP; 1048c2ecf20Sopenharmony_ci if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP; 1058c2ecf20Sopenharmony_ci if (args.kind ) mmu->type[i].type |= NVIF_MEM_KIND; 1068c2ecf20Sopenharmony_ci if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE; 1078c2ecf20Sopenharmony_ci if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT; 1088c2ecf20Sopenharmony_ci if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED; 1098c2ecf20Sopenharmony_ci mmu->type[i].heap = args.heap; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (mmu->kind_nr) { 1138c2ecf20Sopenharmony_ci struct nvif_mmu_kind_v0 *kind; 1148c2ecf20Sopenharmony_ci size_t argc = struct_size(kind, data, mmu->kind_nr); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL))) 1178c2ecf20Sopenharmony_ci goto done; 1188c2ecf20Sopenharmony_ci kind->version = 0; 1198c2ecf20Sopenharmony_ci kind->count = mmu->kind_nr; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND, 1228c2ecf20Sopenharmony_ci kind, argc); 1238c2ecf20Sopenharmony_ci if (ret == 0) 1248c2ecf20Sopenharmony_ci memcpy(mmu->kind, kind->data, kind->count); 1258c2ecf20Sopenharmony_ci mmu->kind_inv = kind->kind_inv; 1268c2ecf20Sopenharmony_ci kfree(kind); 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cidone: 1308c2ecf20Sopenharmony_ci if (ret) 1318c2ecf20Sopenharmony_ci nvif_mmu_dtor(mmu); 1328c2ecf20Sopenharmony_ci return ret; 1338c2ecf20Sopenharmony_ci} 134