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