xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/pmu/nouveau_nvkm_subdev_pmu_base.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_pmu_base.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2013 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  * Authors: Ben Skeggs
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_pmu_base.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $");
28 
29 #include "priv.h"
30 
31 #include <core/firmware.h>
32 #include <subdev/timer.h>
33 
34 bool
nvkm_pmu_fan_controlled(struct nvkm_device * device)35 nvkm_pmu_fan_controlled(struct nvkm_device *device)
36 {
37 	struct nvkm_pmu *pmu = device->pmu;
38 
39 	/* Internal PMU FW does not currently control fans in any way,
40 	 * allow SW control of fans instead.
41 	 */
42 	if (pmu && pmu->func->code.size)
43 		return false;
44 
45 	/* Default (board-loaded, or VBIOS PMU/PREOS) PMU FW on Fermi
46 	 * and newer automatically control the fan speed, which would
47 	 * interfere with SW control.
48 	 */
49 	return (device->chipset >= 0xc0);
50 }
51 
52 void
nvkm_pmu_pgob(struct nvkm_pmu * pmu,bool enable)53 nvkm_pmu_pgob(struct nvkm_pmu *pmu, bool enable)
54 {
55 	if (pmu && pmu->func->pgob)
56 		pmu->func->pgob(pmu, enable);
57 }
58 
59 static void
nvkm_pmu_recv(struct work_struct * work)60 nvkm_pmu_recv(struct work_struct *work)
61 {
62 	struct nvkm_pmu *pmu = container_of(work, typeof(*pmu), recv.work);
63 	return pmu->func->recv(pmu);
64 }
65 
66 int
nvkm_pmu_send(struct nvkm_pmu * pmu,u32 reply[2],u32 process,u32 message,u32 data0,u32 data1)67 nvkm_pmu_send(struct nvkm_pmu *pmu, u32 reply[2],
68 	      u32 process, u32 message, u32 data0, u32 data1)
69 {
70 	if (!pmu || !pmu->func->send)
71 		return -ENODEV;
72 	return pmu->func->send(pmu, reply, process, message, data0, data1);
73 }
74 
75 static void
nvkm_pmu_intr(struct nvkm_subdev * subdev)76 nvkm_pmu_intr(struct nvkm_subdev *subdev)
77 {
78 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
79 	if (!pmu->func->intr)
80 		return;
81 	pmu->func->intr(pmu);
82 }
83 
84 static int
nvkm_pmu_fini(struct nvkm_subdev * subdev,bool suspend)85 nvkm_pmu_fini(struct nvkm_subdev *subdev, bool suspend)
86 {
87 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
88 
89 	if (pmu->func->fini)
90 		pmu->func->fini(pmu);
91 
92 	flush_work(&pmu->recv.work);
93 
94 	reinit_completion(&pmu->wpr_ready);
95 
96 	nvkm_falcon_cmdq_fini(pmu->lpq);
97 	nvkm_falcon_cmdq_fini(pmu->hpq);
98 	pmu->initmsg_received = false;
99 	return 0;
100 }
101 
102 static int
nvkm_pmu_reset(struct nvkm_pmu * pmu)103 nvkm_pmu_reset(struct nvkm_pmu *pmu)
104 {
105 	struct nvkm_device *device = pmu->subdev.device;
106 
107 	if (!pmu->func->enabled(pmu))
108 		return 0;
109 
110 	/* Inhibit interrupts, and wait for idle. */
111 	nvkm_wr32(device, 0x10a014, 0x0000ffff);
112 	nvkm_msec(device, 2000,
113 		if (!nvkm_rd32(device, 0x10a04c))
114 			break;
115 	);
116 
117 	/* Reset. */
118 	if (pmu->func->reset)
119 		pmu->func->reset(pmu);
120 
121 	/* Wait for IMEM/DMEM scrubbing to be complete. */
122 	nvkm_msec(device, 2000,
123 		if (!(nvkm_rd32(device, 0x10a10c) & 0x00000006))
124 			break;
125 	);
126 
127 	return 0;
128 }
129 
130 static int
nvkm_pmu_preinit(struct nvkm_subdev * subdev)131 nvkm_pmu_preinit(struct nvkm_subdev *subdev)
132 {
133 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
134 	return nvkm_pmu_reset(pmu);
135 }
136 
137 static int
nvkm_pmu_init(struct nvkm_subdev * subdev)138 nvkm_pmu_init(struct nvkm_subdev *subdev)
139 {
140 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
141 	int ret = nvkm_pmu_reset(pmu);
142 	if (ret == 0 && pmu->func->init)
143 		ret = pmu->func->init(pmu);
144 	return ret;
145 }
146 
147 static void *
nvkm_pmu_dtor(struct nvkm_subdev * subdev)148 nvkm_pmu_dtor(struct nvkm_subdev *subdev)
149 {
150 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
151 #ifdef __NetBSD__
152 	DRM_DESTROY_WAITQUEUE(&pmu->recv.wait);
153 #endif
154 	nvkm_falcon_msgq_del(&pmu->msgq);
155 	nvkm_falcon_cmdq_del(&pmu->lpq);
156 	nvkm_falcon_cmdq_del(&pmu->hpq);
157 	nvkm_falcon_qmgr_del(&pmu->qmgr);
158 	nvkm_falcon_dtor(&pmu->falcon);
159 	return nvkm_pmu(subdev);
160 }
161 
162 static const struct nvkm_subdev_func
163 nvkm_pmu = {
164 	.dtor = nvkm_pmu_dtor,
165 	.preinit = nvkm_pmu_preinit,
166 	.init = nvkm_pmu_init,
167 	.fini = nvkm_pmu_fini,
168 	.intr = nvkm_pmu_intr,
169 };
170 
171 int
nvkm_pmu_ctor(const struct nvkm_pmu_fwif * fwif,struct nvkm_device * device,int index,struct nvkm_pmu * pmu)172 nvkm_pmu_ctor(const struct nvkm_pmu_fwif *fwif, struct nvkm_device *device,
173 	      int index, struct nvkm_pmu *pmu)
174 {
175 	int ret;
176 
177 	nvkm_subdev_ctor(&nvkm_pmu, device, index, &pmu->subdev);
178 
179 	INIT_WORK(&pmu->recv.work, nvkm_pmu_recv);
180 #ifdef __NetBSD__
181 	DRM_INIT_WAITQUEUE(&pmu->recv.wait, "nvpmu");
182 #else
183 	init_waitqueue_head(&pmu->recv.wait);
184 #endif
185 
186 	fwif = nvkm_firmware_load(&pmu->subdev, fwif, "Pmu", pmu);
187 	if (IS_ERR(fwif))
188 		return PTR_ERR(fwif);
189 
190 	pmu->func = fwif->func;
191 
192 	ret = nvkm_falcon_ctor(pmu->func->flcn, &pmu->subdev,
193 			       nvkm_subdev_name[pmu->subdev.index], 0x10a000,
194 			       &pmu->falcon);
195 	if (ret)
196 		return ret;
197 
198 	if ((ret = nvkm_falcon_qmgr_new(&pmu->falcon, &pmu->qmgr)) ||
199 	    (ret = nvkm_falcon_cmdq_new(pmu->qmgr, "hpq", &pmu->hpq)) ||
200 	    (ret = nvkm_falcon_cmdq_new(pmu->qmgr, "lpq", &pmu->lpq)) ||
201 	    (ret = nvkm_falcon_msgq_new(pmu->qmgr, "msgq", &pmu->msgq)))
202 		return ret;
203 
204 	init_completion(&pmu->wpr_ready);
205 	return 0;
206 }
207 
208 int
nvkm_pmu_new_(const struct nvkm_pmu_fwif * fwif,struct nvkm_device * device,int index,struct nvkm_pmu ** ppmu)209 nvkm_pmu_new_(const struct nvkm_pmu_fwif *fwif, struct nvkm_device *device,
210 	      int index, struct nvkm_pmu **ppmu)
211 {
212 	struct nvkm_pmu *pmu;
213 	if (!(pmu = *ppmu = kzalloc(sizeof(*pmu), GFP_KERNEL)))
214 		return -ENOMEM;
215 	return nvkm_pmu_ctor(fwif, device, index, *ppmu);
216 }
217