xref: /linux/drivers/gpu/drm/nouveau/nvif/mem.c (revision b8d312aa075f33282565467662c4628dae0a2aff)
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/mem.h>
23 #include <nvif/client.h>
24 
25 #include <nvif/if000a.h>
26 
27 int
28 nvif_mem_init_map(struct nvif_mmu *mmu, u8 type, u64 size, struct nvif_mem *mem)
29 {
30 	int ret = nvif_mem_init(mmu, mmu->mem, NVIF_MEM_MAPPABLE | type, 0,
31 				size, NULL, 0, mem);
32 	if (ret == 0) {
33 		ret = nvif_object_map(&mem->object, NULL, 0);
34 		if (ret)
35 			nvif_mem_fini(mem);
36 	}
37 	return ret;
38 }
39 
40 void
41 nvif_mem_fini(struct nvif_mem *mem)
42 {
43 	nvif_object_fini(&mem->object);
44 }
45 
46 int
47 nvif_mem_init_type(struct nvif_mmu *mmu, s32 oclass, int type, u8 page,
48 		   u64 size, void *argv, u32 argc, struct nvif_mem *mem)
49 {
50 	struct nvif_mem_v0 *args;
51 	u8 stack[128];
52 	int ret;
53 
54 	mem->object.client = NULL;
55 	if (type < 0)
56 		return -EINVAL;
57 
58 	if (sizeof(*args) + argc > sizeof(stack)) {
59 		if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
60 			return -ENOMEM;
61 	} else {
62 		args = (void *)stack;
63 	}
64 	args->version = 0;
65 	args->type = type;
66 	args->page = page;
67 	args->size = size;
68 	memcpy(args->data, argv, argc);
69 
70 	ret = nvif_object_init(&mmu->object, 0, oclass, args,
71 			       sizeof(*args) + argc, &mem->object);
72 	if (ret == 0) {
73 		mem->type = mmu->type[type].type;
74 		mem->page = args->page;
75 		mem->addr = args->addr;
76 		mem->size = args->size;
77 	}
78 
79 	if (args != (void *)stack)
80 		kfree(args);
81 	return ret;
82 
83 }
84 
85 int
86 nvif_mem_init(struct nvif_mmu *mmu, s32 oclass, u8 type, u8 page,
87 	      u64 size, void *argv, u32 argc, struct nvif_mem *mem)
88 {
89 	int ret = -EINVAL, i;
90 
91 	mem->object.client = NULL;
92 
93 	for (i = 0; ret && i < mmu->type_nr; i++) {
94 		if ((mmu->type[i].type & type) == type) {
95 			ret = nvif_mem_init_type(mmu, oclass, i, page, size,
96 						 argv, argc, mem);
97 		}
98 	}
99 
100 	return ret;
101 }
102