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