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