xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/pmu/base.c (revision ccdc043123d2a485e173e5e2627598151b7850b3)
1 /*
2  * Copyright 2013 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  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 
26 #include <core/firmware.h>
27 #include <subdev/timer.h>
28 
29 bool
30 nvkm_pmu_fan_controlled(struct nvkm_device *device)
31 {
32 	struct nvkm_pmu *pmu = device->pmu;
33 
34 	/* Internal PMU FW does not currently control fans in any way,
35 	 * allow SW control of fans instead.
36 	 */
37 	if (pmu && pmu->func->code.size)
38 		return false;
39 
40 	/* Default (board-loaded, or VBIOS PMU/PREOS) PMU FW on Fermi
41 	 * and newer automatically control the fan speed, which would
42 	 * interfere with SW control.
43 	 */
44 	return (device->chipset >= 0xc0);
45 }
46 
47 void
48 nvkm_pmu_pgob(struct nvkm_pmu *pmu, bool enable)
49 {
50 	if (pmu && pmu->func->pgob)
51 		pmu->func->pgob(pmu, enable);
52 }
53 
54 static void
55 nvkm_pmu_recv(struct work_struct *work)
56 {
57 	struct nvkm_pmu *pmu = container_of(work, typeof(*pmu), recv.work);
58 	return pmu->func->recv(pmu);
59 }
60 
61 int
62 nvkm_pmu_send(struct nvkm_pmu *pmu, u32 reply[2],
63 	      u32 process, u32 message, u32 data0, u32 data1)
64 {
65 	if (!pmu || !pmu->func->send)
66 		return -ENODEV;
67 	return pmu->func->send(pmu, reply, process, message, data0, data1);
68 }
69 
70 static void
71 nvkm_pmu_intr(struct nvkm_subdev *subdev)
72 {
73 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
74 	if (!pmu->func->intr)
75 		return;
76 	pmu->func->intr(pmu);
77 }
78 
79 static int
80 nvkm_pmu_fini(struct nvkm_subdev *subdev, bool suspend)
81 {
82 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
83 
84 	if (!subdev->use.enabled)
85 		return 0;
86 
87 	if (pmu->func->fini)
88 		pmu->func->fini(pmu);
89 
90 	flush_work(&pmu->recv.work);
91 
92 	reinit_completion(&pmu->wpr_ready);
93 
94 	nvkm_falcon_cmdq_fini(pmu->lpq);
95 	nvkm_falcon_cmdq_fini(pmu->hpq);
96 	pmu->initmsg_received = false;
97 	return 0;
98 }
99 
100 static int
101 nvkm_pmu_init(struct nvkm_subdev *subdev)
102 {
103 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
104 
105 	if (!pmu->func->init)
106 		return 0;
107 
108 	return pmu->func->init(pmu);
109 }
110 
111 static void *
112 nvkm_pmu_dtor(struct nvkm_subdev *subdev)
113 {
114 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
115 	nvkm_falcon_msgq_del(&pmu->msgq);
116 	nvkm_falcon_cmdq_del(&pmu->lpq);
117 	nvkm_falcon_cmdq_del(&pmu->hpq);
118 	nvkm_falcon_qmgr_del(&pmu->qmgr);
119 	nvkm_falcon_dtor(&pmu->falcon);
120 	mutex_destroy(&pmu->send.mutex);
121 	return nvkm_pmu(subdev);
122 }
123 
124 static const struct nvkm_subdev_func
125 nvkm_pmu = {
126 	.dtor = nvkm_pmu_dtor,
127 	.init = nvkm_pmu_init,
128 	.fini = nvkm_pmu_fini,
129 	.intr = nvkm_pmu_intr,
130 };
131 
132 int
133 nvkm_pmu_ctor(const struct nvkm_pmu_fwif *fwif, struct nvkm_device *device,
134 	      enum nvkm_subdev_type type, int inst, struct nvkm_pmu *pmu)
135 {
136 	int ret;
137 
138 	nvkm_subdev_ctor(&nvkm_pmu, device, type, inst, &pmu->subdev);
139 
140 	mutex_init(&pmu->send.mutex);
141 
142 	INIT_WORK(&pmu->recv.work, nvkm_pmu_recv);
143 	init_waitqueue_head(&pmu->recv.wait);
144 
145 	fwif = nvkm_firmware_load(&pmu->subdev, fwif, "Pmu", pmu);
146 	if (IS_ERR(fwif))
147 		return PTR_ERR(fwif);
148 
149 	pmu->func = fwif->func;
150 
151 	ret = nvkm_falcon_ctor(pmu->func->flcn, &pmu->subdev, pmu->subdev.name,
152 			       0x10a000, &pmu->falcon);
153 	if (ret)
154 		return ret;
155 
156 	if ((ret = nvkm_falcon_qmgr_new(&pmu->falcon, &pmu->qmgr)) ||
157 	    (ret = nvkm_falcon_cmdq_new(pmu->qmgr, "hpq", &pmu->hpq)) ||
158 	    (ret = nvkm_falcon_cmdq_new(pmu->qmgr, "lpq", &pmu->lpq)) ||
159 	    (ret = nvkm_falcon_msgq_new(pmu->qmgr, "msgq", &pmu->msgq)))
160 		return ret;
161 
162 	init_completion(&pmu->wpr_ready);
163 	return 0;
164 }
165 
166 int
167 nvkm_pmu_new_(const struct nvkm_pmu_fwif *fwif, struct nvkm_device *device,
168 	      enum nvkm_subdev_type type, int inst, struct nvkm_pmu **ppmu)
169 {
170 	struct nvkm_pmu *pmu;
171 	if (!(pmu = *ppmu = kzalloc(sizeof(*pmu), GFP_KERNEL)))
172 		return -ENOMEM;
173 	return nvkm_pmu_ctor(fwif, device, type, inst, *ppmu);
174 }
175