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