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 void
nvkm_engine_unref(struct nvkm_engine ** pengine)31 nvkm_engine_unref(struct nvkm_engine **pengine)
32 {
33 struct nvkm_engine *engine = *pengine;
34 if (engine) {
35 mutex_lock(&engine->subdev.mutex);
36 if (--engine->usecount == 0)
37 nvkm_subdev_fini(&engine->subdev, false);
38 mutex_unlock(&engine->subdev.mutex);
39 *pengine = NULL;
40 }
41 }
42
43 struct nvkm_engine *
nvkm_engine_ref(struct nvkm_engine * engine)44 nvkm_engine_ref(struct nvkm_engine *engine)
45 {
46 if (engine) {
47 mutex_lock(&engine->subdev.mutex);
48 if (++engine->usecount == 1) {
49 int ret = nvkm_subdev_init(&engine->subdev);
50 if (ret) {
51 engine->usecount--;
52 mutex_unlock(&engine->subdev.mutex);
53 return ERR_PTR(ret);
54 }
55 }
56 mutex_unlock(&engine->subdev.mutex);
57 }
58 return engine;
59 }
60
61 void
nvkm_engine_tile(struct nvkm_engine * engine,int region)62 nvkm_engine_tile(struct nvkm_engine *engine, int region)
63 {
64 struct nvkm_fb *fb = engine->subdev.device->fb;
65 if (engine->func->tile)
66 engine->func->tile(engine, region, &fb->tile.region[region]);
67 }
68
69 static void
nvkm_engine_intr(struct nvkm_subdev * subdev)70 nvkm_engine_intr(struct nvkm_subdev *subdev)
71 {
72 struct nvkm_engine *engine = nvkm_engine(subdev);
73 if (engine->func->intr)
74 engine->func->intr(engine);
75 }
76
77 static int
nvkm_engine_fini(struct nvkm_subdev * subdev,bool suspend)78 nvkm_engine_fini(struct nvkm_subdev *subdev, bool suspend)
79 {
80 struct nvkm_engine *engine = nvkm_engine(subdev);
81 if (engine->func->fini)
82 return engine->func->fini(engine, suspend);
83 return 0;
84 }
85
86 static int
nvkm_engine_init(struct nvkm_subdev * subdev)87 nvkm_engine_init(struct nvkm_subdev *subdev)
88 {
89 struct nvkm_engine *engine = nvkm_engine(subdev);
90 struct nvkm_fb *fb = subdev->device->fb;
91 int ret = 0, i;
92 s64 time;
93
94 if (!engine->usecount) {
95 nvkm_trace(subdev, "init skipped, engine has no users\n");
96 return ret;
97 }
98
99 if (engine->func->oneinit && !engine->subdev.oneinit) {
100 nvkm_trace(subdev, "one-time init running...\n");
101 time = ktime_to_us(ktime_get());
102 ret = engine->func->oneinit(engine);
103 if (ret) {
104 nvkm_trace(subdev, "one-time init failed, %d\n", ret);
105 return ret;
106 }
107
108 engine->subdev.oneinit = true;
109 time = ktime_to_us(ktime_get()) - time;
110 nvkm_trace(subdev, "one-time init completed in %lldus\n", time);
111 }
112
113 if (engine->func->init)
114 ret = engine->func->init(engine);
115
116 for (i = 0; fb && i < fb->tile.regions; i++)
117 nvkm_engine_tile(engine, i);
118 return ret;
119 }
120
121 static void *
nvkm_engine_dtor(struct nvkm_subdev * subdev)122 nvkm_engine_dtor(struct nvkm_subdev *subdev)
123 {
124 struct nvkm_engine *engine = nvkm_engine(subdev);
125 if (engine->func->dtor)
126 return engine->func->dtor(engine);
127 return engine;
128 }
129
130 static const struct nvkm_subdev_func
131 nvkm_engine_func = {
132 .dtor = nvkm_engine_dtor,
133 .init = nvkm_engine_init,
134 .fini = nvkm_engine_fini,
135 .intr = nvkm_engine_intr,
136 };
137
138 int
nvkm_engine_ctor(const struct nvkm_engine_func * func,struct nvkm_device * device,int index,bool enable,struct nvkm_engine * engine)139 nvkm_engine_ctor(const struct nvkm_engine_func *func,
140 struct nvkm_device *device, int index, bool enable,
141 struct nvkm_engine *engine)
142 {
143 nvkm_subdev_ctor(&nvkm_engine_func, device, index, &engine->subdev);
144 engine->func = func;
145
146 if (!nvkm_boolopt(device->cfgopt, nvkm_subdev_name[index], enable)) {
147 nvkm_debug(&engine->subdev, "disabled\n");
148 return -ENODEV;
149 }
150
151 spin_lock_init(&engine->lock);
152 return 0;
153 }
154
155 int
nvkm_engine_new_(const struct nvkm_engine_func * func,struct nvkm_device * device,int index,bool enable,struct nvkm_engine ** pengine)156 nvkm_engine_new_(const struct nvkm_engine_func *func,
157 struct nvkm_device *device, int index, bool enable,
158 struct nvkm_engine **pengine)
159 {
160 if (!(*pengine = kzalloc(sizeof(**pengine), GFP_KERNEL)))
161 return -ENOMEM;
162 return nvkm_engine_ctor(func, device, index, enable, *pengine);
163 }
164