xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/base.c (revision d642ef71)
1 /*
2  * Copyright 2019 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 int
25 nvkm_gsp_intr_nonstall(struct nvkm_gsp *gsp, enum nvkm_subdev_type type, int inst)
26 {
27 	for (int i = 0; i < gsp->intr_nr; i++) {
28 		if (gsp->intr[i].type == type && gsp->intr[i].inst == inst) {
29 			if (gsp->intr[i].nonstall != ~0)
30 				return gsp->intr[i].nonstall;
31 
32 			return -EINVAL;
33 		}
34 	}
35 
36 	return -ENOENT;
37 }
38 
39 int
40 nvkm_gsp_intr_stall(struct nvkm_gsp *gsp, enum nvkm_subdev_type type, int inst)
41 {
42 	for (int i = 0; i < gsp->intr_nr; i++) {
43 		if (gsp->intr[i].type == type && gsp->intr[i].inst == inst) {
44 			if (gsp->intr[i].stall != ~0)
45 				return gsp->intr[i].stall;
46 
47 			return -EINVAL;
48 		}
49 	}
50 
51 	return -ENOENT;
52 }
53 
54 static int
55 nvkm_gsp_fini(struct nvkm_subdev *subdev, bool suspend)
56 {
57 	struct nvkm_gsp *gsp = nvkm_gsp(subdev);
58 
59 	if (!gsp->func->fini)
60 		return 0;
61 
62 	return gsp->func->fini(gsp, suspend);
63 }
64 
65 static int
66 nvkm_gsp_init(struct nvkm_subdev *subdev)
67 {
68 	struct nvkm_gsp *gsp = nvkm_gsp(subdev);
69 
70 	if (!gsp->func->init)
71 		return 0;
72 
73 	return gsp->func->init(gsp);
74 }
75 
76 static int
77 nvkm_gsp_oneinit(struct nvkm_subdev *subdev)
78 {
79 	struct nvkm_gsp *gsp = nvkm_gsp(subdev);
80 
81 	if (!gsp->func->oneinit)
82 		return 0;
83 
84 	return gsp->func->oneinit(gsp);
85 }
86 
87 static void *
88 nvkm_gsp_dtor(struct nvkm_subdev *subdev)
89 {
90 	struct nvkm_gsp *gsp = nvkm_gsp(subdev);
91 
92 	if (gsp->func && gsp->func->dtor)
93 		gsp->func->dtor(gsp);
94 
95 	nvkm_falcon_dtor(&gsp->falcon);
96 	return gsp;
97 }
98 
99 static const struct nvkm_subdev_func
100 nvkm_gsp = {
101 	.dtor = nvkm_gsp_dtor,
102 	.oneinit = nvkm_gsp_oneinit,
103 	.init = nvkm_gsp_init,
104 	.fini = nvkm_gsp_fini,
105 };
106 
107 int
108 nvkm_gsp_new_(const struct nvkm_gsp_fwif *fwif, struct nvkm_device *device,
109 	      enum nvkm_subdev_type type, int inst, struct nvkm_gsp **pgsp)
110 {
111 	struct nvkm_gsp *gsp;
112 
113 	if (!(gsp = *pgsp = kzalloc(sizeof(*gsp), GFP_KERNEL)))
114 		return -ENOMEM;
115 
116 	nvkm_subdev_ctor(&nvkm_gsp, device, type, inst, &gsp->subdev);
117 
118 	fwif = nvkm_firmware_load(&gsp->subdev, fwif, "Gsp", gsp);
119 	if (IS_ERR(fwif))
120 		return PTR_ERR(fwif);
121 
122 	gsp->func = fwif->func;
123 	gsp->rm = gsp->func->rm;
124 
125 	return nvkm_falcon_ctor(gsp->func->flcn, &gsp->subdev, gsp->subdev.name, 0x110000,
126 				&gsp->falcon);
127 }
128