1 /*
2  * Copyright 2015 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 <bskeggs@redhat.com>
23  */
24 #include "priv.h"
25 
26 #include <engine/fifo.h>
27 
28 static bool
29 nvkm_gr_chsw_load(struct nvkm_engine *engine)
30 {
31 	struct nvkm_gr *gr = nvkm_gr(engine);
32 	if (gr->func->chsw_load)
33 		return gr->func->chsw_load(gr);
34 	return false;
35 }
36 
37 static void
38 nvkm_gr_tile(struct nvkm_engine *engine, int region, struct nvkm_fb_tile *tile)
39 {
40 	struct nvkm_gr *gr = nvkm_gr(engine);
41 	if (gr->func->tile)
42 		gr->func->tile(gr, region, tile);
43 }
44 
45 u64
46 nvkm_gr_units(struct nvkm_gr *gr)
47 {
48 	if (gr->func->units)
49 		return gr->func->units(gr);
50 	return 0;
51 }
52 
53 int
54 nvkm_gr_tlb_flush(struct nvkm_gr *gr)
55 {
56 	if (gr->func->tlb_flush)
57 		return gr->func->tlb_flush(gr);
58 	return -ENODEV;
59 }
60 
61 static int
62 nvkm_gr_oclass_get(struct nvkm_oclass *oclass, int index)
63 {
64 	struct nvkm_gr *gr = nvkm_gr(oclass->engine);
65 	int c = 0;
66 
67 	if (gr->func->object_get) {
68 		int ret = gr->func->object_get(gr, index, &oclass->base);
69 		if (oclass->base.oclass)
70 			return index;
71 		return ret;
72 	}
73 
74 	while (gr->func->sclass[c].oclass) {
75 		if (c++ == index) {
76 			oclass->base = gr->func->sclass[index];
77 			return index;
78 		}
79 	}
80 
81 	return c;
82 }
83 
84 static int
85 nvkm_gr_cclass_new(struct nvkm_fifo_chan *chan,
86 		   const struct nvkm_oclass *oclass,
87 		   struct nvkm_object **pobject)
88 {
89 	struct nvkm_gr *gr = nvkm_gr(oclass->engine);
90 	if (gr->func->chan_new)
91 		return gr->func->chan_new(gr, chan, oclass, pobject);
92 	return 0;
93 }
94 
95 static void
96 nvkm_gr_intr(struct nvkm_engine *engine)
97 {
98 	struct nvkm_gr *gr = nvkm_gr(engine);
99 	gr->func->intr(gr);
100 }
101 
102 static int
103 nvkm_gr_oneinit(struct nvkm_engine *engine)
104 {
105 	struct nvkm_gr *gr = nvkm_gr(engine);
106 	if (gr->func->oneinit)
107 		return gr->func->oneinit(gr);
108 	return 0;
109 }
110 
111 static int
112 nvkm_gr_init(struct nvkm_engine *engine)
113 {
114 	struct nvkm_gr *gr = nvkm_gr(engine);
115 	return gr->func->init(gr);
116 }
117 
118 static int
119 nvkm_gr_fini(struct nvkm_engine *engine, bool suspend)
120 {
121 	struct nvkm_gr *gr = nvkm_gr(engine);
122 	if (gr->func->fini)
123 		return gr->func->fini(gr, suspend);
124 	return 0;
125 }
126 
127 static void *
128 nvkm_gr_dtor(struct nvkm_engine *engine)
129 {
130 	struct nvkm_gr *gr = nvkm_gr(engine);
131 	if (gr->func->dtor)
132 		return gr->func->dtor(gr);
133 	return gr;
134 }
135 
136 static const struct nvkm_engine_func
137 nvkm_gr = {
138 	.dtor = nvkm_gr_dtor,
139 	.oneinit = nvkm_gr_oneinit,
140 	.init = nvkm_gr_init,
141 	.fini = nvkm_gr_fini,
142 	.intr = nvkm_gr_intr,
143 	.tile = nvkm_gr_tile,
144 	.chsw_load = nvkm_gr_chsw_load,
145 	.fifo.cclass = nvkm_gr_cclass_new,
146 	.fifo.sclass = nvkm_gr_oclass_get,
147 };
148 
149 int
150 nvkm_gr_ctor(const struct nvkm_gr_func *func, struct nvkm_device *device,
151 	     int index, bool enable, struct nvkm_gr *gr)
152 {
153 	gr->func = func;
154 	return nvkm_engine_ctor(&nvkm_gr, device, index, enable, &gr->engine);
155 }
156