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