1 /*
2  * Copyright 2018 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 #include "wndw.h"
23 #include "atom.h"
24 
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_plane_helper.h>
27 #include <nouveau_bo.h>
28 
29 #include <nvif/clc37e.h>
30 
31 static void
32 wndwc57e_ilut_clr(struct nv50_wndw *wndw)
33 {
34 	u32 *push;
35 	if ((push = evo_wait(&wndw->wndw, 2))) {
36 		evo_mthd(push, 0x0444, 1);
37 		evo_data(push, 0x00000000);
38 		evo_kick(push, &wndw->wndw);
39 	}
40 }
41 
42 static void
43 wndwc57e_ilut_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
44 {
45 	u32 *push;
46 	if ((push = evo_wait(&wndw->wndw, 4))) {
47 		evo_mthd(push, 0x0440, 3);
48 		evo_data(push, asyw->xlut.i.size << 8 |
49 			       asyw->xlut.i.mode << 2 |
50 			       asyw->xlut.i.output_mode);
51 		evo_data(push, asyw->xlut.handle);
52 		evo_data(push, asyw->xlut.i.offset >> 8);
53 		evo_kick(push, &wndw->wndw);
54 	}
55 }
56 
57 static u16
58 fixedU0_16_FP16(u16 fixed)
59 {
60         int sign = 0, exp = 0, man = 0;
61         if (fixed) {
62                 while (--exp && !(fixed & 0x8000))
63                         fixed <<= 1;
64                 man = ((fixed << 1) & 0xffc0) >> 6;
65                 exp += 15;
66         }
67         return (sign << 15) | (exp << 10) | man;
68 }
69 
70 static void
71 wndwc57e_ilut_load(struct drm_color_lut *in, int size, void __iomem *mem)
72 {
73 	memset_io(mem, 0x00, 0x20); /* VSS header. */
74 	mem += 0x20;
75 
76 	for (; size--; in++, mem += 0x08) {
77 		u16 r = fixedU0_16_FP16(drm_color_lut_extract(in->  red, 16));
78 		u16 g = fixedU0_16_FP16(drm_color_lut_extract(in->green, 16));
79 		u16 b = fixedU0_16_FP16(drm_color_lut_extract(in-> blue, 16));
80 		writew(r, mem + 0);
81 		writew(g, mem + 2);
82 		writew(b, mem + 4);
83 	}
84 
85 	/* INTERPOLATE modes require a "next" entry to interpolate with,
86 	 * so we replicate the last entry to deal with this for now.
87 	 */
88 	writew(readw(mem - 8), mem + 0);
89 	writew(readw(mem - 6), mem + 2);
90 	writew(readw(mem - 4), mem + 4);
91 }
92 
93 static void
94 wndwc57e_ilut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
95 {
96 	u16 size = asyw->ilut->length / sizeof(struct drm_color_lut);
97 	if (size == 256) {
98 		asyw->xlut.i.mode = 1; /* DIRECT8. */
99 	} else {
100 		asyw->xlut.i.mode = 2; /* DIRECT10. */
101 		size = 1024;
102 	}
103 	asyw->xlut.i.size = 4 /* VSS header. */ + size + 1 /* Entries. */;
104 	asyw->xlut.i.output_mode = 0; /* INTERPOLATE_DISABLE. */
105 	asyw->xlut.i.load = wndwc57e_ilut_load;
106 }
107 
108 static const struct nv50_wndw_func
109 wndwc57e = {
110 	.acquire = wndwc37e_acquire,
111 	.release = wndwc37e_release,
112 	.sema_set = wndwc37e_sema_set,
113 	.sema_clr = wndwc37e_sema_clr,
114 	.ntfy_set = wndwc37e_ntfy_set,
115 	.ntfy_clr = wndwc37e_ntfy_clr,
116 	.ntfy_reset = corec37d_ntfy_init,
117 	.ntfy_wait_begun = base507c_ntfy_wait_begun,
118 	.ilut = wndwc57e_ilut,
119 	.ilut_identity = true,
120 	.xlut_set = wndwc57e_ilut_set,
121 	.xlut_clr = wndwc57e_ilut_clr,
122 	.image_set = wndwc37e_image_set,
123 	.image_clr = wndwc37e_image_clr,
124 	.update = wndwc37e_update,
125 };
126 
127 int
128 wndwc57e_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
129 	     s32 oclass, struct nv50_wndw **pwndw)
130 {
131 	return wndwc37e_new_(&wndwc57e, drm, type, index, oclass,
132 			     BIT(index >> 1), pwndw);
133 }
134