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