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 <engine/fifo.h>
27
28 #include <nvrm/nvtypes.h>
29 #include <nvrm/535.113.01/common/sdk/nvidia/inc/nvos.h>
30
31 struct r535_nvenc_obj {
32 struct nvkm_object object;
33 struct nvkm_gsp_object rm;
34 };
35
36 static void *
r535_nvenc_obj_dtor(struct nvkm_object * object)37 r535_nvenc_obj_dtor(struct nvkm_object *object)
38 {
39 struct r535_nvenc_obj *obj = container_of(object, typeof(*obj), object);
40
41 nvkm_gsp_rm_free(&obj->rm);
42 return obj;
43 }
44
45 static const struct nvkm_object_func
46 r535_nvenc_obj = {
47 .dtor = r535_nvenc_obj_dtor,
48 };
49
50 static int
r535_nvenc_obj_ctor(const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)51 r535_nvenc_obj_ctor(const struct nvkm_oclass *oclass, void *argv, u32 argc,
52 struct nvkm_object **pobject)
53 {
54 struct nvkm_chan *chan = nvkm_uchan_chan(oclass->parent);
55 struct r535_nvenc_obj *obj;
56 NV_MSENC_ALLOCATION_PARAMETERS *args;
57
58 if (!(obj = kzalloc(sizeof(*obj), GFP_KERNEL)))
59 return -ENOMEM;
60
61 nvkm_object_ctor(&r535_nvenc_obj, oclass, &obj->object);
62 *pobject = &obj->object;
63
64 args = nvkm_gsp_rm_alloc_get(&chan->rm.object, oclass->handle, oclass->base.oclass,
65 sizeof(*args), &obj->rm);
66 if (WARN_ON(IS_ERR(args)))
67 return PTR_ERR(args);
68
69 args->size = sizeof(*args);
70 args->engineInstance = oclass->engine->subdev.inst;
71
72 return nvkm_gsp_rm_alloc_wr(&obj->rm, args);
73 }
74
75 static void *
r535_nvenc_dtor(struct nvkm_engine * engine)76 r535_nvenc_dtor(struct nvkm_engine *engine)
77 {
78 struct nvkm_nvenc *nvenc = nvkm_nvenc(engine);
79
80 kfree(nvenc->engine.func);
81 return nvenc;
82 }
83
84 int
r535_nvenc_new(const struct nvkm_engine_func * hw,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_nvenc ** pnvenc)85 r535_nvenc_new(const struct nvkm_engine_func *hw, struct nvkm_device *device,
86 enum nvkm_subdev_type type, int inst, struct nvkm_nvenc **pnvenc)
87 {
88 struct nvkm_engine_func *rm;
89 int nclass;
90
91 for (nclass = 0; hw->sclass[nclass].oclass; nclass++);
92
93 if (!(rm = kzalloc(sizeof(*rm) + (nclass + 1) * sizeof(rm->sclass[0]), GFP_KERNEL)))
94 return -ENOMEM;
95
96 rm->dtor = r535_nvenc_dtor;
97 for (int i = 0; i < nclass; i++) {
98 rm->sclass[i].minver = hw->sclass[i].minver;
99 rm->sclass[i].maxver = hw->sclass[i].maxver;
100 rm->sclass[i].oclass = hw->sclass[i].oclass;
101 rm->sclass[i].ctor = r535_nvenc_obj_ctor;
102 }
103
104 if (!(*pnvenc = kzalloc(sizeof(**pnvenc), GFP_KERNEL))) {
105 kfree(rm);
106 return -ENOMEM;
107 }
108
109 return nvkm_engine_ctor(rm, device, type, inst, true, &(*pnvenc)->engine);
110 }
111