1 /*
2  * Copyright 2012 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 <core/subdev.h>
25 #include <core/device.h>
26 #include <core/option.h>
27 #include <subdev/mc.h>
28 
29 static struct lock_class_key nvkm_subdev_lock_class[NVKM_SUBDEV_NR];
30 
31 const char *
32 nvkm_subdev_name[NVKM_SUBDEV_NR] = {
33 	[NVKM_SUBDEV_BAR     ] = "bar",
34 	[NVKM_SUBDEV_VBIOS   ] = "bios",
35 	[NVKM_SUBDEV_BUS     ] = "bus",
36 	[NVKM_SUBDEV_CLK     ] = "clk",
37 	[NVKM_SUBDEV_DEVINIT ] = "devinit",
38 	[NVKM_SUBDEV_FB      ] = "fb",
39 	[NVKM_SUBDEV_FUSE    ] = "fuse",
40 	[NVKM_SUBDEV_GPIO    ] = "gpio",
41 	[NVKM_SUBDEV_I2C     ] = "i2c",
42 	[NVKM_SUBDEV_IBUS    ] = "priv",
43 	[NVKM_SUBDEV_ICCSENSE] = "iccsense",
44 	[NVKM_SUBDEV_INSTMEM ] = "imem",
45 	[NVKM_SUBDEV_LTC     ] = "ltc",
46 	[NVKM_SUBDEV_MC      ] = "mc",
47 	[NVKM_SUBDEV_MMU     ] = "mmu",
48 	[NVKM_SUBDEV_MXM     ] = "mxm",
49 	[NVKM_SUBDEV_PCI     ] = "pci",
50 	[NVKM_SUBDEV_PMU     ] = "pmu",
51 	[NVKM_SUBDEV_SECBOOT ] = "secboot",
52 	[NVKM_SUBDEV_THERM   ] = "therm",
53 	[NVKM_SUBDEV_TIMER   ] = "tmr",
54 	[NVKM_SUBDEV_TOP     ] = "top",
55 	[NVKM_SUBDEV_VOLT    ] = "volt",
56 	[NVKM_ENGINE_BSP     ] = "bsp",
57 	[NVKM_ENGINE_CE0     ] = "ce0",
58 	[NVKM_ENGINE_CE1     ] = "ce1",
59 	[NVKM_ENGINE_CE2     ] = "ce2",
60 	[NVKM_ENGINE_CE3     ] = "ce3",
61 	[NVKM_ENGINE_CE4     ] = "ce4",
62 	[NVKM_ENGINE_CE5     ] = "ce5",
63 	[NVKM_ENGINE_CIPHER  ] = "cipher",
64 	[NVKM_ENGINE_DISP    ] = "disp",
65 	[NVKM_ENGINE_DMAOBJ  ] = "dma",
66 	[NVKM_ENGINE_FIFO    ] = "fifo",
67 	[NVKM_ENGINE_GR      ] = "gr",
68 	[NVKM_ENGINE_IFB     ] = "ifb",
69 	[NVKM_ENGINE_ME      ] = "me",
70 	[NVKM_ENGINE_MPEG    ] = "mpeg",
71 	[NVKM_ENGINE_MSENC   ] = "msenc",
72 	[NVKM_ENGINE_MSPDEC  ] = "mspdec",
73 	[NVKM_ENGINE_MSPPP   ] = "msppp",
74 	[NVKM_ENGINE_MSVLD   ] = "msvld",
75 	[NVKM_ENGINE_NVENC0  ] = "nvenc0",
76 	[NVKM_ENGINE_NVENC1  ] = "nvenc1",
77 	[NVKM_ENGINE_NVENC2  ] = "nvenc2",
78 	[NVKM_ENGINE_NVDEC   ] = "nvdec",
79 	[NVKM_ENGINE_PM      ] = "pm",
80 	[NVKM_ENGINE_SEC     ] = "sec",
81 	[NVKM_ENGINE_SW      ] = "sw",
82 	[NVKM_ENGINE_VIC     ] = "vic",
83 	[NVKM_ENGINE_VP      ] = "vp",
84 };
85 
86 void
87 nvkm_subdev_intr(struct nvkm_subdev *subdev)
88 {
89 	if (subdev->func->intr)
90 		subdev->func->intr(subdev);
91 }
92 
93 int
94 nvkm_subdev_fini(struct nvkm_subdev *subdev, bool suspend)
95 {
96 	struct nvkm_device *device = subdev->device;
97 	const char *action = suspend ? "suspend" : "fini";
98 	s64 time;
99 
100 	nvkm_trace(subdev, "%s running...\n", action);
101 	time = ktime_to_us(ktime_get());
102 
103 	if (subdev->func->fini) {
104 		int ret = subdev->func->fini(subdev, suspend);
105 		if (ret) {
106 			nvkm_error(subdev, "%s failed, %d\n", action, ret);
107 			if (suspend)
108 				return ret;
109 		}
110 	}
111 
112 	nvkm_mc_reset(device, subdev->index);
113 
114 	time = ktime_to_us(ktime_get()) - time;
115 	nvkm_trace(subdev, "%s completed in %lldus\n", action, time);
116 	return 0;
117 }
118 
119 int
120 nvkm_subdev_preinit(struct nvkm_subdev *subdev)
121 {
122 	s64 time;
123 
124 	nvkm_trace(subdev, "preinit running...\n");
125 	time = ktime_to_us(ktime_get());
126 
127 	if (subdev->func->preinit) {
128 		int ret = subdev->func->preinit(subdev);
129 		if (ret) {
130 			nvkm_error(subdev, "preinit failed, %d\n", ret);
131 			return ret;
132 		}
133 	}
134 
135 	time = ktime_to_us(ktime_get()) - time;
136 	nvkm_trace(subdev, "preinit completed in %lldus\n", time);
137 	return 0;
138 }
139 
140 int
141 nvkm_subdev_init(struct nvkm_subdev *subdev)
142 {
143 	s64 time;
144 	int ret;
145 
146 	nvkm_trace(subdev, "init running...\n");
147 	time = ktime_to_us(ktime_get());
148 
149 	if (subdev->func->oneinit && !subdev->oneinit) {
150 		s64 time;
151 		nvkm_trace(subdev, "one-time init running...\n");
152 		time = ktime_to_us(ktime_get());
153 		ret = subdev->func->oneinit(subdev);
154 		if (ret) {
155 			nvkm_error(subdev, "one-time init failed, %d\n", ret);
156 			return ret;
157 		}
158 
159 		subdev->oneinit = true;
160 		time = ktime_to_us(ktime_get()) - time;
161 		nvkm_trace(subdev, "one-time init completed in %lldus\n", time);
162 	}
163 
164 	if (subdev->func->init) {
165 		ret = subdev->func->init(subdev);
166 		if (ret) {
167 			nvkm_error(subdev, "init failed, %d\n", ret);
168 			return ret;
169 		}
170 	}
171 
172 	time = ktime_to_us(ktime_get()) - time;
173 	nvkm_trace(subdev, "init completed in %lldus\n", time);
174 	return 0;
175 }
176 
177 void
178 nvkm_subdev_del(struct nvkm_subdev **psubdev)
179 {
180 	struct nvkm_subdev *subdev = *psubdev;
181 	s64 time;
182 
183 	if (subdev && !WARN_ON(!subdev->func)) {
184 		nvkm_trace(subdev, "destroy running...\n");
185 		time = ktime_to_us(ktime_get());
186 		if (subdev->func->dtor)
187 			*psubdev = subdev->func->dtor(subdev);
188 		time = ktime_to_us(ktime_get()) - time;
189 		nvkm_trace(subdev, "destroy completed in %lldus\n", time);
190 		kfree(*psubdev);
191 		*psubdev = NULL;
192 	}
193 }
194 
195 void
196 nvkm_subdev_ctor(const struct nvkm_subdev_func *func,
197 		 struct nvkm_device *device, int index,
198 		 struct nvkm_subdev *subdev)
199 {
200 	const char *name = nvkm_subdev_name[index];
201 	subdev->func = func;
202 	subdev->device = device;
203 	subdev->index = index;
204 
205 	__mutex_init(&subdev->mutex, name, &nvkm_subdev_lock_class[index]);
206 	subdev->debug = nvkm_dbgopt(device->dbgopt, name);
207 }
208