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/msgqueue.h>
27 #include <subdev/timer.h>
28 
29 void
30 nvkm_pmu_pgob(struct nvkm_pmu *pmu, bool enable)
31 {
32 	if (pmu && pmu->func->pgob)
33 		pmu->func->pgob(pmu, enable);
34 }
35 
36 static void
37 nvkm_pmu_recv(struct work_struct *work)
38 {
39 	struct nvkm_pmu *pmu = container_of(work, typeof(*pmu), recv.work);
40 	return pmu->func->recv(pmu);
41 }
42 
43 int
44 nvkm_pmu_send(struct nvkm_pmu *pmu, u32 reply[2],
45 	      u32 process, u32 message, u32 data0, u32 data1)
46 {
47 	if (!pmu || !pmu->func->send)
48 		return -ENODEV;
49 	return pmu->func->send(pmu, reply, process, message, data0, data1);
50 }
51 
52 static void
53 nvkm_pmu_intr(struct nvkm_subdev *subdev)
54 {
55 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
56 	if (!pmu->func->intr)
57 		return;
58 	pmu->func->intr(pmu);
59 }
60 
61 static int
62 nvkm_pmu_fini(struct nvkm_subdev *subdev, bool suspend)
63 {
64 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
65 
66 	if (pmu->func->fini)
67 		pmu->func->fini(pmu);
68 
69 	flush_work(&pmu->recv.work);
70 	return 0;
71 }
72 
73 static int
74 nvkm_pmu_reset(struct nvkm_pmu *pmu)
75 {
76 	struct nvkm_device *device = pmu->subdev.device;
77 
78 	if (!pmu->func->enabled(pmu))
79 		return 0;
80 
81 	/* Inhibit interrupts, and wait for idle. */
82 	nvkm_wr32(device, 0x10a014, 0x0000ffff);
83 	nvkm_msec(device, 2000,
84 		if (!nvkm_rd32(device, 0x10a04c))
85 			break;
86 	);
87 
88 	/* Reset. */
89 	if (pmu->func->reset)
90 		pmu->func->reset(pmu);
91 
92 	/* Wait for IMEM/DMEM scrubbing to be complete. */
93 	nvkm_msec(device, 2000,
94 		if (!(nvkm_rd32(device, 0x10a10c) & 0x00000006))
95 			break;
96 	);
97 
98 	return 0;
99 }
100 
101 static int
102 nvkm_pmu_preinit(struct nvkm_subdev *subdev)
103 {
104 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
105 	return nvkm_pmu_reset(pmu);
106 }
107 
108 static int
109 nvkm_pmu_init(struct nvkm_subdev *subdev)
110 {
111 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
112 	int ret = nvkm_pmu_reset(pmu);
113 	if (ret == 0 && pmu->func->init)
114 		ret = pmu->func->init(pmu);
115 	return ret;
116 }
117 
118 static int
119 nvkm_pmu_oneinit(struct nvkm_subdev *subdev)
120 {
121 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
122 	return nvkm_falcon_v1_new(&pmu->subdev, "PMU", 0x10a000, &pmu->falcon);
123 }
124 
125 static void *
126 nvkm_pmu_dtor(struct nvkm_subdev *subdev)
127 {
128 	struct nvkm_pmu *pmu = nvkm_pmu(subdev);
129 	nvkm_msgqueue_del(&pmu->queue);
130 	nvkm_falcon_del(&pmu->falcon);
131 	return nvkm_pmu(subdev);
132 }
133 
134 static const struct nvkm_subdev_func
135 nvkm_pmu = {
136 	.dtor = nvkm_pmu_dtor,
137 	.preinit = nvkm_pmu_preinit,
138 	.oneinit = nvkm_pmu_oneinit,
139 	.init = nvkm_pmu_init,
140 	.fini = nvkm_pmu_fini,
141 	.intr = nvkm_pmu_intr,
142 };
143 
144 int
145 nvkm_pmu_ctor(const struct nvkm_pmu_func *func, struct nvkm_device *device,
146 	      int index, struct nvkm_pmu *pmu)
147 {
148 	nvkm_subdev_ctor(&nvkm_pmu, device, index, &pmu->subdev);
149 	pmu->func = func;
150 	INIT_WORK(&pmu->recv.work, nvkm_pmu_recv);
151 	init_waitqueue_head(&pmu->recv.wait);
152 	return 0;
153 }
154 
155 int
156 nvkm_pmu_new_(const struct nvkm_pmu_func *func, struct nvkm_device *device,
157 	      int index, struct nvkm_pmu **ppmu)
158 {
159 	struct nvkm_pmu *pmu;
160 	if (!(pmu = *ppmu = kzalloc(sizeof(*pmu), GFP_KERNEL)))
161 		return -ENOMEM;
162 	return nvkm_pmu_ctor(func, device, index, *ppmu);
163 }
164