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
nvkm_gsp_intr_nonstall(struct nvkm_gsp * gsp,enum nvkm_subdev_type type,int inst)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 return gsp->intr[i].nonstall;
30 }
31
32 return -ENOENT;
33 }
34
35 int
nvkm_gsp_intr_stall(struct nvkm_gsp * gsp,enum nvkm_subdev_type type,int inst)36 nvkm_gsp_intr_stall(struct nvkm_gsp *gsp, enum nvkm_subdev_type type, int inst)
37 {
38 for (int i = 0; i < gsp->intr_nr; i++) {
39 if (gsp->intr[i].type == type && gsp->intr[i].inst == inst) {
40 if (gsp->intr[i].stall != ~0)
41 return gsp->intr[i].stall;
42
43 return -EINVAL;
44 }
45 }
46
47 return -ENOENT;
48 }
49
50 static int
nvkm_gsp_fini(struct nvkm_subdev * subdev,bool suspend)51 nvkm_gsp_fini(struct nvkm_subdev *subdev, bool suspend)
52 {
53 struct nvkm_gsp *gsp = nvkm_gsp(subdev);
54
55 if (!gsp->func->fini || !gsp->running)
56 return 0;
57
58 return gsp->func->fini(gsp, suspend);
59 }
60
61 static int
nvkm_gsp_init(struct nvkm_subdev * subdev)62 nvkm_gsp_init(struct nvkm_subdev *subdev)
63 {
64 struct nvkm_gsp *gsp = nvkm_gsp(subdev);
65
66 if (!gsp->func->init)
67 return 0;
68
69 return gsp->func->init(gsp);
70 }
71
72 static int
nvkm_gsp_oneinit(struct nvkm_subdev * subdev)73 nvkm_gsp_oneinit(struct nvkm_subdev *subdev)
74 {
75 struct nvkm_gsp *gsp = nvkm_gsp(subdev);
76
77 if (!gsp->func->oneinit)
78 return 0;
79
80 return gsp->func->oneinit(gsp);
81 }
82
83 void
nvkm_gsp_dtor_fws(struct nvkm_gsp * gsp)84 nvkm_gsp_dtor_fws(struct nvkm_gsp *gsp)
85 {
86 nvkm_firmware_put(gsp->fws.fmc);
87 gsp->fws.fmc = NULL;
88 nvkm_firmware_put(gsp->fws.bl);
89 gsp->fws.bl = NULL;
90 nvkm_firmware_put(gsp->fws.booter.unload);
91 gsp->fws.booter.unload = NULL;
92 nvkm_firmware_put(gsp->fws.booter.load);
93 gsp->fws.booter.load = NULL;
94 nvkm_firmware_put(gsp->fws.rm);
95 gsp->fws.rm = NULL;
96 }
97
98 static void *
nvkm_gsp_dtor(struct nvkm_subdev * subdev)99 nvkm_gsp_dtor(struct nvkm_subdev *subdev)
100 {
101 struct nvkm_gsp *gsp = nvkm_gsp(subdev);
102
103 if (gsp->func && gsp->func->dtor)
104 gsp->func->dtor(gsp);
105
106 nvkm_falcon_dtor(&gsp->falcon);
107 kfree(gsp->rm);
108 return gsp;
109 }
110
111 static const struct nvkm_subdev_func
112 nvkm_gsp = {
113 .dtor = nvkm_gsp_dtor,
114 .oneinit = nvkm_gsp_oneinit,
115 .init = nvkm_gsp_init,
116 .fini = nvkm_gsp_fini,
117 };
118
119 int
nvkm_gsp_load_fw(struct nvkm_gsp * gsp,const char * name,const char * ver,const struct firmware ** pfw)120 nvkm_gsp_load_fw(struct nvkm_gsp *gsp, const char *name, const char *ver,
121 const struct firmware **pfw)
122 {
123 char fwname[64];
124
125 snprintf(fwname, sizeof(fwname), "gsp/%s-%s", name, ver);
126 return nvkm_firmware_get(&gsp->subdev, fwname, 0, pfw);
127 }
128
129 int
nvkm_gsp_new_(const struct nvkm_gsp_fwif * fwif,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_gsp ** pgsp)130 nvkm_gsp_new_(const struct nvkm_gsp_fwif *fwif, struct nvkm_device *device,
131 enum nvkm_subdev_type type, int inst, struct nvkm_gsp **pgsp)
132 {
133 struct nvkm_gsp *gsp;
134
135 if (!(gsp = *pgsp = kzalloc(sizeof(*gsp), GFP_KERNEL)))
136 return -ENOMEM;
137
138 nvkm_subdev_ctor(&nvkm_gsp, device, type, inst, &gsp->subdev);
139
140 fwif = nvkm_firmware_load(&gsp->subdev, fwif, "Gsp", gsp);
141 if (IS_ERR(fwif))
142 return PTR_ERR(fwif);
143
144 gsp->func = fwif->func;
145
146 if (fwif->rm) {
147 nvkm_info(&gsp->subdev, "RM version: %s\n", fwif->ver);
148
149 gsp->rm = kzalloc(sizeof(*gsp->rm), GFP_KERNEL);
150 if (!gsp->rm)
151 return -ENOMEM;
152
153 gsp->rm->device = device;
154 gsp->rm->gpu = fwif->func->rm.gpu;
155 gsp->rm->wpr = fwif->rm->wpr;
156 gsp->rm->api = fwif->rm->api;
157 }
158
159 return nvkm_falcon_ctor(gsp->func->flcn, &gsp->subdev, gsp->subdev.name, 0x110000,
160 &gsp->falcon);
161 }
162