1 /*
2  * Copyright 2014 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 <core/memory.h>
27 
28 void
29 nvkm_ltc_tags_clear(struct nvkm_device *device, u32 first, u32 count)
30 {
31 	struct nvkm_ltc *ltc = device->ltc;
32 	const u32 limit = first + count - 1;
33 
34 	BUG_ON((first > limit) || (limit >= ltc->num_tags));
35 
36 	mutex_lock(&ltc->subdev.mutex);
37 	ltc->func->cbc_clear(ltc, first, limit);
38 	ltc->func->cbc_wait(ltc);
39 	mutex_unlock(&ltc->subdev.mutex);
40 }
41 
42 int
43 nvkm_ltc_zbc_color_get(struct nvkm_ltc *ltc, int index, const u32 color[4])
44 {
45 	memcpy(ltc->zbc_color[index], color, sizeof(ltc->zbc_color[index]));
46 	ltc->func->zbc_clear_color(ltc, index, color);
47 	return index;
48 }
49 
50 int
51 nvkm_ltc_zbc_depth_get(struct nvkm_ltc *ltc, int index, const u32 depth)
52 {
53 	ltc->zbc_depth[index] = depth;
54 	ltc->func->zbc_clear_depth(ltc, index, depth);
55 	return index;
56 }
57 
58 int
59 nvkm_ltc_zbc_stencil_get(struct nvkm_ltc *ltc, int index, const u32 stencil)
60 {
61 	ltc->zbc_stencil[index] = stencil;
62 	ltc->func->zbc_clear_stencil(ltc, index, stencil);
63 	return index;
64 }
65 
66 void
67 nvkm_ltc_invalidate(struct nvkm_ltc *ltc)
68 {
69 	if (ltc->func->invalidate)
70 		ltc->func->invalidate(ltc);
71 }
72 
73 void
74 nvkm_ltc_flush(struct nvkm_ltc *ltc)
75 {
76 	if (ltc->func->flush)
77 		ltc->func->flush(ltc);
78 }
79 
80 static void
81 nvkm_ltc_intr(struct nvkm_subdev *subdev)
82 {
83 	struct nvkm_ltc *ltc = nvkm_ltc(subdev);
84 	ltc->func->intr(ltc);
85 }
86 
87 static int
88 nvkm_ltc_oneinit(struct nvkm_subdev *subdev)
89 {
90 	struct nvkm_ltc *ltc = nvkm_ltc(subdev);
91 	return ltc->func->oneinit(ltc);
92 }
93 
94 static int
95 nvkm_ltc_init(struct nvkm_subdev *subdev)
96 {
97 	struct nvkm_ltc *ltc = nvkm_ltc(subdev);
98 	int i;
99 
100 	for (i = ltc->zbc_min; i <= ltc->zbc_max; i++) {
101 		ltc->func->zbc_clear_color(ltc, i, ltc->zbc_color[i]);
102 		ltc->func->zbc_clear_depth(ltc, i, ltc->zbc_depth[i]);
103 		if (ltc->func->zbc_clear_stencil)
104 			ltc->func->zbc_clear_stencil(ltc, i, ltc->zbc_stencil[i]);
105 	}
106 
107 	ltc->func->init(ltc);
108 	return 0;
109 }
110 
111 static void *
112 nvkm_ltc_dtor(struct nvkm_subdev *subdev)
113 {
114 	struct nvkm_ltc *ltc = nvkm_ltc(subdev);
115 	nvkm_memory_unref(&ltc->tag_ram);
116 	return ltc;
117 }
118 
119 static const struct nvkm_subdev_func
120 nvkm_ltc = {
121 	.dtor = nvkm_ltc_dtor,
122 	.oneinit = nvkm_ltc_oneinit,
123 	.init = nvkm_ltc_init,
124 	.intr = nvkm_ltc_intr,
125 };
126 
127 int
128 nvkm_ltc_new_(const struct nvkm_ltc_func *func, struct nvkm_device *device,
129 	      int index, struct nvkm_ltc **pltc)
130 {
131 	struct nvkm_ltc *ltc;
132 
133 	if (!(ltc = *pltc = kzalloc(sizeof(*ltc), GFP_KERNEL)))
134 		return -ENOMEM;
135 
136 	nvkm_subdev_ctor(&nvkm_ltc, device, index, &ltc->subdev);
137 	ltc->func = func;
138 	ltc->zbc_min = 1; /* reserve 0 for disabled */
139 	ltc->zbc_max = min(func->zbc, NVKM_LTC_MAX_ZBC_CNT) - 1;
140 	return 0;
141 }
142