xref: /linux/drivers/gpu/drm/nouveau/nvif/mmu.c (revision 9ac596a4e875e28bb1fa4b2e00549fadfaf4784e)
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
28eea5cf0fSBen Skeggs nvif_mmu_fini(struct nvif_mmu *mmu)
29eea5cf0fSBen Skeggs {
30eea5cf0fSBen Skeggs 	kfree(mmu->kind);
31eea5cf0fSBen Skeggs 	kfree(mmu->type);
32eea5cf0fSBen Skeggs 	kfree(mmu->heap);
33*9ac596a4SBen Skeggs 	nvif_object_dtor(&mmu->object);
34eea5cf0fSBen Skeggs }
35eea5cf0fSBen Skeggs 
36eea5cf0fSBen Skeggs int
37eea5cf0fSBen Skeggs nvif_mmu_init(struct nvif_object *parent, s32 oclass, struct nvif_mmu *mmu)
38eea5cf0fSBen Skeggs {
39f5650478SBen Skeggs 	static const struct nvif_mclass mems[] = {
40f5650478SBen Skeggs 		{ NVIF_CLASS_MEM_GF100, -1 },
41f5650478SBen Skeggs 		{ NVIF_CLASS_MEM_NV50 , -1 },
42f5650478SBen Skeggs 		{ NVIF_CLASS_MEM_NV04 , -1 },
43f5650478SBen Skeggs 		{}
44f5650478SBen Skeggs 	};
45eea5cf0fSBen Skeggs 	struct nvif_mmu_v0 args;
46eea5cf0fSBen Skeggs 	int ret, i;
47eea5cf0fSBen Skeggs 
48eea5cf0fSBen Skeggs 	args.version = 0;
49eea5cf0fSBen Skeggs 	mmu->heap = NULL;
50eea5cf0fSBen Skeggs 	mmu->type = NULL;
51eea5cf0fSBen Skeggs 	mmu->kind = NULL;
52eea5cf0fSBen Skeggs 
53*9ac596a4SBen Skeggs 	ret = nvif_object_ctor(parent, "nvifMmu", 0, oclass, &args,
54*9ac596a4SBen Skeggs 			       sizeof(args), &mmu->object);
55eea5cf0fSBen Skeggs 	if (ret)
56eea5cf0fSBen Skeggs 		goto done;
57eea5cf0fSBen Skeggs 
58eea5cf0fSBen Skeggs 	mmu->dmabits = args.dmabits;
59eea5cf0fSBen Skeggs 	mmu->heap_nr = args.heap_nr;
60eea5cf0fSBen Skeggs 	mmu->type_nr = args.type_nr;
61eea5cf0fSBen Skeggs 	mmu->kind_nr = args.kind_nr;
62eea5cf0fSBen Skeggs 
63f5650478SBen Skeggs 	ret = nvif_mclass(&mmu->object, mems);
64f5650478SBen Skeggs 	if (ret < 0)
65f5650478SBen Skeggs 		goto done;
66f5650478SBen Skeggs 	mmu->mem = mems[ret].oclass;
67f5650478SBen Skeggs 
686da2ec56SKees Cook 	mmu->heap = kmalloc_array(mmu->heap_nr, sizeof(*mmu->heap),
696da2ec56SKees Cook 				  GFP_KERNEL);
706da2ec56SKees Cook 	mmu->type = kmalloc_array(mmu->type_nr, sizeof(*mmu->type),
716da2ec56SKees Cook 				  GFP_KERNEL);
72eea5cf0fSBen Skeggs 	if (ret = -ENOMEM, !mmu->heap || !mmu->type)
73eea5cf0fSBen Skeggs 		goto done;
74eea5cf0fSBen Skeggs 
756da2ec56SKees Cook 	mmu->kind = kmalloc_array(mmu->kind_nr, sizeof(*mmu->kind),
766da2ec56SKees Cook 				  GFP_KERNEL);
77eea5cf0fSBen Skeggs 	if (!mmu->kind && mmu->kind_nr)
78eea5cf0fSBen Skeggs 		goto done;
79eea5cf0fSBen Skeggs 
80eea5cf0fSBen Skeggs 	for (i = 0; i < mmu->heap_nr; i++) {
81eea5cf0fSBen Skeggs 		struct nvif_mmu_heap_v0 args = { .index = i };
82eea5cf0fSBen Skeggs 
83eea5cf0fSBen Skeggs 		ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_HEAP,
84eea5cf0fSBen Skeggs 				       &args, sizeof(args));
85eea5cf0fSBen Skeggs 		if (ret)
86eea5cf0fSBen Skeggs 			goto done;
87eea5cf0fSBen Skeggs 
88eea5cf0fSBen Skeggs 		mmu->heap[i].size = args.size;
89eea5cf0fSBen Skeggs 	}
90eea5cf0fSBen Skeggs 
91eea5cf0fSBen Skeggs 	for (i = 0; i < mmu->type_nr; i++) {
92eea5cf0fSBen Skeggs 		struct nvif_mmu_type_v0 args = { .index = i };
93eea5cf0fSBen Skeggs 
94eea5cf0fSBen Skeggs 		ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_TYPE,
95eea5cf0fSBen Skeggs 				       &args, sizeof(args));
96eea5cf0fSBen Skeggs 		if (ret)
97eea5cf0fSBen Skeggs 			goto done;
98eea5cf0fSBen Skeggs 
99eea5cf0fSBen Skeggs 		mmu->type[i].type = 0;
100eea5cf0fSBen Skeggs 		if (args.vram) mmu->type[i].type |= NVIF_MEM_VRAM;
101eea5cf0fSBen Skeggs 		if (args.host) mmu->type[i].type |= NVIF_MEM_HOST;
102eea5cf0fSBen Skeggs 		if (args.comp) mmu->type[i].type |= NVIF_MEM_COMP;
103eea5cf0fSBen Skeggs 		if (args.disp) mmu->type[i].type |= NVIF_MEM_DISP;
104eea5cf0fSBen Skeggs 		if (args.kind    ) mmu->type[i].type |= NVIF_MEM_KIND;
105eea5cf0fSBen Skeggs 		if (args.mappable) mmu->type[i].type |= NVIF_MEM_MAPPABLE;
106eea5cf0fSBen Skeggs 		if (args.coherent) mmu->type[i].type |= NVIF_MEM_COHERENT;
107eea5cf0fSBen Skeggs 		if (args.uncached) mmu->type[i].type |= NVIF_MEM_UNCACHED;
108eea5cf0fSBen Skeggs 		mmu->type[i].heap = args.heap;
109eea5cf0fSBen Skeggs 	}
110eea5cf0fSBen Skeggs 
111eea5cf0fSBen Skeggs 	if (mmu->kind_nr) {
112eea5cf0fSBen Skeggs 		struct nvif_mmu_kind_v0 *kind;
1137b974925SGustavo A. R. Silva 		size_t argc = struct_size(kind, data, mmu->kind_nr);
114eea5cf0fSBen Skeggs 
115eea5cf0fSBen Skeggs 		if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
116eea5cf0fSBen Skeggs 			goto done;
117eea5cf0fSBen Skeggs 		kind->version = 0;
118eea5cf0fSBen Skeggs 		kind->count = mmu->kind_nr;
119eea5cf0fSBen Skeggs 
120eea5cf0fSBen Skeggs 		ret = nvif_object_mthd(&mmu->object, NVIF_MMU_V0_KIND,
121eea5cf0fSBen Skeggs 				       kind, argc);
122eea5cf0fSBen Skeggs 		if (ret == 0)
123eea5cf0fSBen Skeggs 			memcpy(mmu->kind, kind->data, kind->count);
124176ada03SJames Jones 		mmu->kind_inv = kind->kind_inv;
125eea5cf0fSBen Skeggs 		kfree(kind);
126eea5cf0fSBen Skeggs 	}
127eea5cf0fSBen Skeggs 
128eea5cf0fSBen Skeggs done:
129eea5cf0fSBen Skeggs 	if (ret)
130eea5cf0fSBen Skeggs 		nvif_mmu_fini(mmu);
131eea5cf0fSBen Skeggs 	return ret;
132eea5cf0fSBen Skeggs }
133