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
nvkm_ltc_tags_clear(struct nvkm_device * device,u32 first,u32 count)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(<c->mutex);
37 ltc->func->cbc_clear(ltc, first, limit);
38 ltc->func->cbc_wait(ltc);
39 mutex_unlock(<c->mutex);
40 }
41
42 int
nvkm_ltc_zbc_color_get(struct nvkm_ltc * ltc,int index,const u32 color[4])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
nvkm_ltc_zbc_depth_get(struct nvkm_ltc * ltc,int index,const u32 depth)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
nvkm_ltc_zbc_stencil_get(struct nvkm_ltc * ltc,int index,const u32 stencil)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
nvkm_ltc_invalidate(struct nvkm_ltc * ltc)67 nvkm_ltc_invalidate(struct nvkm_ltc *ltc)
68 {
69 if (ltc->func->invalidate)
70 ltc->func->invalidate(ltc);
71 }
72
73 void
nvkm_ltc_flush(struct nvkm_ltc * ltc)74 nvkm_ltc_flush(struct nvkm_ltc *ltc)
75 {
76 if (ltc->func->flush)
77 ltc->func->flush(ltc);
78 }
79
80 static void
nvkm_ltc_intr(struct nvkm_subdev * subdev)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
nvkm_ltc_oneinit(struct nvkm_subdev * subdev)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
nvkm_ltc_init(struct nvkm_subdev * subdev)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_color_min; i <= ltc->zbc_color_max; i++)
101 ltc->func->zbc_clear_color(ltc, i, ltc->zbc_color[i]);
102
103 for (i = ltc->zbc_depth_min; i <= ltc->zbc_depth_max; i++) {
104 ltc->func->zbc_clear_depth(ltc, i, ltc->zbc_depth[i]);
105 if (ltc->func->zbc_clear_stencil)
106 ltc->func->zbc_clear_stencil(ltc, i, ltc->zbc_stencil[i]);
107 }
108
109 ltc->func->init(ltc);
110 return 0;
111 }
112
113 static void *
nvkm_ltc_dtor(struct nvkm_subdev * subdev)114 nvkm_ltc_dtor(struct nvkm_subdev *subdev)
115 {
116 struct nvkm_ltc *ltc = nvkm_ltc(subdev);
117 nvkm_memory_unref(<c->tag_ram);
118 mutex_destroy(<c->mutex);
119 return ltc;
120 }
121
122 static const struct nvkm_subdev_func
123 nvkm_ltc = {
124 .dtor = nvkm_ltc_dtor,
125 .oneinit = nvkm_ltc_oneinit,
126 .init = nvkm_ltc_init,
127 .intr = nvkm_ltc_intr,
128 };
129
130 int
nvkm_ltc_new_(const struct nvkm_ltc_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_ltc ** pltc)131 nvkm_ltc_new_(const struct nvkm_ltc_func *func, struct nvkm_device *device,
132 enum nvkm_subdev_type type, int inst, struct nvkm_ltc **pltc)
133 {
134 struct nvkm_ltc *ltc;
135
136 if (!(ltc = *pltc = kzalloc(sizeof(*ltc), GFP_KERNEL)))
137 return -ENOMEM;
138
139 nvkm_subdev_ctor(&nvkm_ltc, device, type, inst, <c->subdev);
140 ltc->func = func;
141 mutex_init(<c->mutex);
142 ltc->zbc_color_min = 1; /* reserve 0 for disabled */
143 ltc->zbc_color_max = min(func->zbc_color, NVKM_LTC_MAX_ZBC_COLOR_CNT) - 1;
144 ltc->zbc_depth_min = 1; /* reserve 0 for disabled */
145 ltc->zbc_depth_max = min(func->zbc_depth, NVKM_LTC_MAX_ZBC_DEPTH_CNT) - 1;
146 return 0;
147 }
148