Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2017 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  */
0022 #include <nvif/mmu.h>
0023 
0024 #include <nvif/class.h>
0025 #include <nvif/if0008.h>
0026 
0027 void
0028 nvif_mmu_dtor(struct nvif_mmu *mmu)
0029 {
0030     kfree(mmu->kind);
0031     kfree(mmu->type);
0032     kfree(mmu->heap);
0033     nvif_object_dtor(&mmu->object);
0034 }
0035 
0036 int
0037 nvif_mmu_ctor(struct nvif_object *parent, const char *name, s32 oclass,
0038           struct nvif_mmu *mmu)
0039 {
0040     static const struct nvif_mclass mems[] = {
0041         { NVIF_CLASS_MEM_GF100, -1 },
0042         { NVIF_CLASS_MEM_NV50 , -1 },
0043         { NVIF_CLASS_MEM_NV04 , -1 },
0044         {}
0045     };
0046     struct nvif_mmu_v0 args;
0047     int ret, i;
0048 
0049     args.version = 0;
0050     mmu->heap = NULL;
0051     mmu->type = NULL;
0052     mmu->kind = NULL;
0053 
0054     ret = nvif_object_ctor(parent, name ? name : "nvifMmu", 0, oclass,
0055                    &args, sizeof(args), &mmu->object);
0056     if (ret)
0057         goto done;
0058 
0059     mmu->dmabits = args.dmabits;
0060     mmu->heap_nr = args.heap_nr;
0061     mmu->type_nr = args.type_nr;
0062     mmu->kind_nr = args.kind_nr;
0063 
0064     ret = nvif_mclass(&mmu->object, mems);
0065     if (ret < 0)
0066         goto done;
0067     mmu->mem = mems[ret].oclass;
0068 
0069     mmu->heap = kmalloc_array(mmu->heap_nr, sizeof(*mmu->heap),
0070                   GFP_KERNEL);
0071     mmu->type = kmalloc_array(mmu->type_nr, sizeof(*mmu->type),
0072                   GFP_KERNEL);
0073     if (ret = -ENOMEM, !mmu->heap || !mmu->type)
0074         goto done;
0075 
0076     mmu->kind = kmalloc_array(mmu->kind_nr, sizeof(*mmu->kind),
0077                   GFP_KERNEL);
0078     if (!mmu->kind && mmu->kind_nr)
0079         goto done;
0080 
0081     for (i = 0; i < mmu->heap_nr; i++) {
0082         struct nvif_mmu_heap_v0 args = { .index = i };
0083 
0084         ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP,
0085                        &args, sizeof(args));
0086         if (ret)
0087             goto done;
0088 
0089         mmu->heap[i].size = args.size;
0090     }
0091 
0092     for (i = 0; i < mmu->type_nr; i++) {
0093         struct nvif_mmu_type_v0 args = { .index = i };
0094 
0095         ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE,
0096                        &args, sizeof(args));
0097         if (ret)
0098             goto done;
0099 
0100         mmu->type[i].type = 0;
0101         if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM;
0102         if (args.host) mmu->type[i].type |= NVIF_MEM_HOST;
0103         if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP;
0104         if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP;
0105         if (args.kind    ) mmu->type[i].type |= NVIF_MEM_KIND;
0106         if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE;
0107         if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT;
0108         if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED;
0109         mmu->type[i].heap = args.heap;
0110     }
0111 
0112     if (mmu->kind_nr) {
0113         struct nvif_mmu_kind_v0 *kind;
0114         size_t argc = struct_size(kind, data, mmu->kind_nr);
0115 
0116         if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
0117             goto done;
0118         kind->version = 0;
0119         kind->count = mmu->kind_nr;
0120 
0121         ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND,
0122                        kind, argc);
0123         if (ret == 0)
0124             memcpy(mmu->kind, kind->data, kind->count);
0125         mmu->kind_inv = kind->kind_inv;
0126         kfree(kind);
0127     }
0128 
0129 done:
0130     if (ret)
0131         nvif_mmu_dtor(mmu);
0132     return ret;
0133 }