1 /* $NetBSD: nouveau_nvif_mem.c,v 1.2 2021/12/18 23:45:33 riastradh Exp $ */
2
3 /*
4 * Copyright 2017 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvif_mem.c,v 1.2 2021/12/18 23:45:33 riastradh Exp $");
26
27 #include <nvif/mem.h>
28 #include <nvif/client.h>
29
30 #include <nvif/if000a.h>
31
32 int
nvif_mem_init_map(struct nvif_mmu * mmu,u8 type,u64 size,struct nvif_mem * mem)33 nvif_mem_init_map(struct nvif_mmu *mmu, u8 type, u64 size, struct nvif_mem *mem)
34 {
35 int ret = nvif_mem_init(mmu, mmu->mem, NVIF_MEM_MAPPABLE | type, 0,
36 size, NULL, 0, mem);
37 if (ret == 0) {
38 ret = nvif_object_map(&mem->object, NULL, 0);
39 if (ret)
40 nvif_mem_fini(mem);
41 }
42 return ret;
43 }
44
45 void
nvif_mem_fini(struct nvif_mem * mem)46 nvif_mem_fini(struct nvif_mem *mem)
47 {
48 nvif_object_fini(&mem->object);
49 }
50
51 int
nvif_mem_init_type(struct nvif_mmu * mmu,s32 oclass,int type,u8 page,u64 size,void * argv,u32 argc,struct nvif_mem * mem)52 nvif_mem_init_type(struct nvif_mmu *mmu, s32 oclass, int type, u8 page,
53 u64 size, void *argv, u32 argc, struct nvif_mem *mem)
54 {
55 struct nvif_mem_v0 *args;
56 u8 stack[128];
57 int ret;
58
59 mem->object.client = NULL;
60 if (type < 0)
61 return -EINVAL;
62
63 if (sizeof(*args) + argc > sizeof(stack)) {
64 if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
65 return -ENOMEM;
66 } else {
67 args = (void *)stack;
68 }
69 args->version = 0;
70 args->type = type;
71 args->page = page;
72 args->size = size;
73 memcpy(args->data, argv, argc);
74
75 ret = nvif_object_init(&mmu->object, 0, oclass, args,
76 sizeof(*args) + argc, &mem->object);
77 if (ret == 0) {
78 mem->type = mmu->type[type].type;
79 mem->page = args->page;
80 mem->addr = args->addr;
81 mem->size = args->size;
82 }
83
84 if (args != (void *)stack)
85 kfree(args);
86 return ret;
87
88 }
89
90 int
nvif_mem_init(struct nvif_mmu * mmu,s32 oclass,u8 type,u8 page,u64 size,void * argv,u32 argc,struct nvif_mem * mem)91 nvif_mem_init(struct nvif_mmu *mmu, s32 oclass, u8 type, u8 page,
92 u64 size, void *argv, u32 argc, struct nvif_mem *mem)
93 {
94 int ret = -EINVAL, i;
95
96 mem->object.client = NULL;
97
98 for (i = 0; ret && i < mmu->type_nr; i++) {
99 if ((mmu->type[i].type & type) == type) {
100 ret = nvif_mem_init_type(mmu, oclass, i, page, size,
101 argv, argc, mem);
102 }
103 }
104
105 return ret;
106 }
107