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