xref: /linux/drivers/gpu/drm/nouveau/nvkm/engine/ofa/r535.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 /*
2  * Copyright 2023 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 "priv.h"
23 
24 #include <core/object.h>
25 #include <subdev/gsp.h>
26 #include <subdev/mmu.h>
27 #include <engine/fifo.h>
28 
29 #include <nvrm/nvtypes.h>
30 #include <nvrm/535.113.01/common/sdk/nvidia/inc/nvos.h>
31 
32 struct r535_ofa_obj {
33 	struct nvkm_object object;
34 	struct nvkm_gsp_object rm;
35 };
36 
37 static void *
38 r535_ofa_obj_dtor(struct nvkm_object *object)
39 {
40 	struct r535_ofa_obj *obj = container_of(object, typeof(*obj), object);
41 
42 	nvkm_gsp_rm_free(&obj->rm);
43 	return obj;
44 }
45 
46 static const struct nvkm_object_func
47 r535_ofa_obj = {
48 	.dtor = r535_ofa_obj_dtor,
49 };
50 
51 static int
52 r535_ofa_obj_ctor(const struct nvkm_oclass *oclass, void *argv, u32 argc,
53 		 struct nvkm_object **pobject)
54 {
55 	struct nvkm_chan *chan = nvkm_uchan_chan(oclass->parent);
56 	struct r535_ofa_obj *obj;
57 	NV_OFA_ALLOCATION_PARAMETERS *args;
58 
59 	if (!(obj = kzalloc(sizeof(*obj), GFP_KERNEL)))
60 		return -ENOMEM;
61 
62 	nvkm_object_ctor(&r535_ofa_obj, oclass, &obj->object);
63 	*pobject = &obj->object;
64 
65 	args = nvkm_gsp_rm_alloc_get(&chan->rm.object, oclass->handle, oclass->base.oclass,
66 				     sizeof(*args), &obj->rm);
67 	if (WARN_ON(IS_ERR(args)))
68 		return PTR_ERR(args);
69 
70 	args->size = sizeof(*args);
71 
72 	return nvkm_gsp_rm_alloc_wr(&obj->rm, args);
73 }
74 
75 static void *
76 r535_ofa_dtor(struct nvkm_engine *engine)
77 {
78 	kfree(engine->func);
79 	return engine;
80 }
81 
82 int
83 r535_ofa_new(const struct nvkm_engine_func *hw, struct nvkm_device *device,
84 	     enum nvkm_subdev_type type, int inst, struct nvkm_engine **pengine)
85 {
86 	struct nvkm_engine_func *rm;
87 	int nclass, ret;
88 
89 	for (nclass = 0; hw->sclass[nclass].oclass; nclass++);
90 
91 	if (!(rm = kzalloc(sizeof(*rm) + (nclass + 1) * sizeof(rm->sclass[0]), GFP_KERNEL)))
92 		return -ENOMEM;
93 
94 	rm->dtor = r535_ofa_dtor;
95 	for (int i = 0; i < nclass; i++) {
96 		rm->sclass[i].minver = hw->sclass[i].minver;
97 		rm->sclass[i].maxver = hw->sclass[i].maxver;
98 		rm->sclass[i].oclass = hw->sclass[i].oclass;
99 		rm->sclass[i].ctor = r535_ofa_obj_ctor;
100 	}
101 
102 	ret = nvkm_engine_new_(rm, device, type, inst, true, pengine);
103 	if (ret)
104 		kfree(rm);
105 
106 	return ret;
107 }
108