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/engine.h>
25 #include <core/device.h>
26 #include <core/option.h>
27 
28 #include <subdev/fb.h>
29 
30 bool
nvkm_engine_chsw_load(struct nvkm_engine * engine)31 nvkm_engine_chsw_load(struct nvkm_engine *engine)
32 {
33 	if (engine->func->chsw_load)
34 		return engine->func->chsw_load(engine);
35 	return false;
36 }
37 
38 int
nvkm_engine_reset(struct nvkm_engine * engine)39 nvkm_engine_reset(struct nvkm_engine *engine)
40 {
41 	if (engine->func->reset)
42 		return engine->func->reset(engine);
43 
44 	nvkm_subdev_fini(&engine->subdev, false);
45 	return nvkm_subdev_init(&engine->subdev);
46 }
47 
48 void
nvkm_engine_unref(struct nvkm_engine ** pengine)49 nvkm_engine_unref(struct nvkm_engine **pengine)
50 {
51 	struct nvkm_engine *engine = *pengine;
52 
53 	if (engine) {
54 		nvkm_subdev_unref(&engine->subdev);
55 		*pengine = NULL;
56 	}
57 }
58 
59 struct nvkm_engine *
nvkm_engine_ref(struct nvkm_engine * engine)60 nvkm_engine_ref(struct nvkm_engine *engine)
61 {
62 	int ret;
63 
64 	if (engine) {
65 		ret = nvkm_subdev_ref(&engine->subdev);
66 		if (ret)
67 			return ERR_PTR(ret);
68 	}
69 
70 	return engine;
71 }
72 
73 void
nvkm_engine_tile(struct nvkm_engine * engine,int region)74 nvkm_engine_tile(struct nvkm_engine *engine, int region)
75 {
76 	struct nvkm_fb *fb = engine->subdev.device->fb;
77 	if (engine->func->tile)
78 		engine->func->tile(engine, region, &fb->tile.region[region]);
79 }
80 
81 static void
nvkm_engine_intr(struct nvkm_subdev * subdev)82 nvkm_engine_intr(struct nvkm_subdev *subdev)
83 {
84 	struct nvkm_engine *engine = nvkm_engine(subdev);
85 	if (engine->func->intr)
86 		engine->func->intr(engine);
87 }
88 
89 static int
nvkm_engine_info(struct nvkm_subdev * subdev,u64 mthd,u64 * data)90 nvkm_engine_info(struct nvkm_subdev *subdev, u64 mthd, u64 *data)
91 {
92 	struct nvkm_engine *engine = nvkm_engine(subdev);
93 
94 	if (engine->func->info)
95 		return engine->func->info(engine, mthd, data);
96 
97 	return -ENOSYS;
98 }
99 
100 static int
nvkm_engine_fini(struct nvkm_subdev * subdev,bool suspend)101 nvkm_engine_fini(struct nvkm_subdev *subdev, bool suspend)
102 {
103 	struct nvkm_engine *engine = nvkm_engine(subdev);
104 	if (engine->func->fini)
105 		return engine->func->fini(engine, suspend);
106 	return 0;
107 }
108 
109 static int
nvkm_engine_init(struct nvkm_subdev * subdev)110 nvkm_engine_init(struct nvkm_subdev *subdev)
111 {
112 	struct nvkm_engine *engine = nvkm_engine(subdev);
113 	struct nvkm_fb *fb = subdev->device->fb;
114 	int ret = 0, i;
115 
116 	if (engine->func->init)
117 		ret = engine->func->init(engine);
118 
119 	for (i = 0; fb && i < fb->tile.regions; i++)
120 		nvkm_engine_tile(engine, i);
121 	return ret;
122 }
123 
124 static int
nvkm_engine_oneinit(struct nvkm_subdev * subdev)125 nvkm_engine_oneinit(struct nvkm_subdev *subdev)
126 {
127 	struct nvkm_engine *engine = nvkm_engine(subdev);
128 
129 	if (engine->func->oneinit)
130 		return engine->func->oneinit(engine);
131 
132 	return 0;
133 }
134 
135 static int
nvkm_engine_preinit(struct nvkm_subdev * subdev)136 nvkm_engine_preinit(struct nvkm_subdev *subdev)
137 {
138 	struct nvkm_engine *engine = nvkm_engine(subdev);
139 	if (engine->func->preinit)
140 		engine->func->preinit(engine);
141 	return 0;
142 }
143 
144 static void *
nvkm_engine_dtor(struct nvkm_subdev * subdev)145 nvkm_engine_dtor(struct nvkm_subdev *subdev)
146 {
147 	struct nvkm_engine *engine = nvkm_engine(subdev);
148 	if (engine->func->dtor)
149 		return engine->func->dtor(engine);
150 	return engine;
151 }
152 
153 const struct nvkm_subdev_func
154 nvkm_engine = {
155 	.dtor = nvkm_engine_dtor,
156 	.preinit = nvkm_engine_preinit,
157 	.oneinit = nvkm_engine_oneinit,
158 	.init = nvkm_engine_init,
159 	.fini = nvkm_engine_fini,
160 	.info = nvkm_engine_info,
161 	.intr = nvkm_engine_intr,
162 };
163 
164 int
nvkm_engine_ctor(const struct nvkm_engine_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,bool enable,struct nvkm_engine * engine)165 nvkm_engine_ctor(const struct nvkm_engine_func *func, struct nvkm_device *device,
166 		 enum nvkm_subdev_type type, int inst, bool enable, struct nvkm_engine *engine)
167 {
168 	engine->func = func;
169 	nvkm_subdev_ctor(&nvkm_engine, device, type, inst, &engine->subdev);
170 	refcount_set(&engine->subdev.use.refcount, 0);
171 
172 	if (!nvkm_boolopt(device->cfgopt, engine->subdev.name, enable)) {
173 		nvkm_debug(&engine->subdev, "disabled\n");
174 		return -ENODEV;
175 	}
176 
177 	spin_lock_init(&engine->lock);
178 	return 0;
179 }
180 
181 int
nvkm_engine_new_(const struct nvkm_engine_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,bool enable,struct nvkm_engine ** pengine)182 nvkm_engine_new_(const struct nvkm_engine_func *func, struct nvkm_device *device,
183 		 enum nvkm_subdev_type type, int inst, bool enable,
184 		 struct nvkm_engine **pengine)
185 {
186 	if (!(*pengine = kzalloc(sizeof(**pengine), GFP_KERNEL)))
187 		return -ENOMEM;
188 	return nvkm_engine_ctor(func, device, type, inst, enable, *pengine);
189 }
190