18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2018 Red Hat Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#include "head.h"
238c2ecf20Sopenharmony_ci#include "atom.h"
248c2ecf20Sopenharmony_ci#include "core.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <nvif/pushc37b.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <nvhw/class/clc57d.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int
318c2ecf20Sopenharmony_ciheadc57d_or(struct nv50_head *head, struct nv50_head_atom *asyh)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
348c2ecf20Sopenharmony_ci	const int i = head->base.index;
358c2ecf20Sopenharmony_ci	u8 depth;
368c2ecf20Sopenharmony_ci	int ret;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/*XXX: This is a dirty hack until OR depth handling is
398c2ecf20Sopenharmony_ci	 *     improved later for deep colour etc.
408c2ecf20Sopenharmony_ci	 */
418c2ecf20Sopenharmony_ci	switch (asyh->or.depth) {
428c2ecf20Sopenharmony_ci	case 6: depth = 5; break;
438c2ecf20Sopenharmony_ci	case 5: depth = 4; break;
448c2ecf20Sopenharmony_ci	case 2: depth = 1; break;
458c2ecf20Sopenharmony_ci	case 0:	depth = 4; break;
468c2ecf20Sopenharmony_ci	default:
478c2ecf20Sopenharmony_ci		depth = asyh->or.depth;
488c2ecf20Sopenharmony_ci		WARN_ON(1);
498c2ecf20Sopenharmony_ci		break;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
538c2ecf20Sopenharmony_ci		return ret;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE(i),
568c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, CRC_MODE, asyh->or.crc_raster) |
578c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, HSYNC_POLARITY, asyh->or.nhsync) |
588c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, VSYNC_POLARITY, asyh->or.nvsync) |
598c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, PIXEL_DEPTH, depth) |
608c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, COLOR_SPACE_OVERRIDE, DISABLE) |
618c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_CONTROL_OUTPUT_RESOURCE, EXT_PACKET_WIN, NONE));
628c2ecf20Sopenharmony_ci	return 0;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int
668c2ecf20Sopenharmony_ciheadc57d_procamp(struct nv50_head *head, struct nv50_head_atom *asyh)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
698c2ecf20Sopenharmony_ci	const int i = head->base.index;
708c2ecf20Sopenharmony_ci	int ret;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
738c2ecf20Sopenharmony_ci		return ret;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	//TODO:
768c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PROCAMP(i),
778c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, COLOR_SPACE, RGB) |
788c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, CHROMA_LPF, DISABLE) |
798c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_PROCAMP, DYNAMIC_RANGE, VESA));
808c2ecf20Sopenharmony_ci	return 0;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int
848c2ecf20Sopenharmony_ciheadc57d_olut_clr(struct nv50_head *head)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
878c2ecf20Sopenharmony_ci	const int i = head->base.index;
888c2ecf20Sopenharmony_ci	int ret;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
918c2ecf20Sopenharmony_ci		return ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_CONTEXT_DMA_OLUT(i), 0x00000000);
948c2ecf20Sopenharmony_ci	return 0;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int
988c2ecf20Sopenharmony_ciheadc57d_olut_set(struct nv50_head *head, struct nv50_head_atom *asyh)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
1018c2ecf20Sopenharmony_ci	const int i = head->base.index;
1028c2ecf20Sopenharmony_ci	int ret;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 5)))
1058c2ecf20Sopenharmony_ci		return ret;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_OLUT_CONTROL(i),
1088c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, INTERPOLATE, asyh->olut.output_mode) |
1098c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_OLUT_CONTROL, MIRROR, DISABLE) |
1108c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, MODE, asyh->olut.mode) |
1118c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_OLUT_CONTROL, SIZE, asyh->olut.size),
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci				HEAD_SET_OLUT_FP_NORM_SCALE(i), 0xffffffff,
1148c2ecf20Sopenharmony_ci				HEAD_SET_CONTEXT_DMA_OLUT(i), asyh->olut.handle,
1158c2ecf20Sopenharmony_ci				HEAD_SET_OFFSET_OLUT(i), asyh->olut.offset >> 8);
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic void
1208c2ecf20Sopenharmony_ciheadc57d_olut_load_8(struct drm_color_lut *in, int size, void __iomem *mem)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	memset_io(mem, 0x00, 0x20); /* VSS header. */
1238c2ecf20Sopenharmony_ci	mem += 0x20;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	while (size--) {
1268c2ecf20Sopenharmony_ci		u16 r = drm_color_lut_extract(in->  red + 0, 16);
1278c2ecf20Sopenharmony_ci		u16 g = drm_color_lut_extract(in->green + 0, 16);
1288c2ecf20Sopenharmony_ci		u16 b = drm_color_lut_extract(in-> blue + 0, 16);
1298c2ecf20Sopenharmony_ci		u16 ri = 0, gi = 0, bi = 0, i;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		if (in++, size) {
1328c2ecf20Sopenharmony_ci			ri = (drm_color_lut_extract(in->  red, 16) - r) / 4;
1338c2ecf20Sopenharmony_ci			gi = (drm_color_lut_extract(in->green, 16) - g) / 4;
1348c2ecf20Sopenharmony_ci			bi = (drm_color_lut_extract(in-> blue, 16) - b) / 4;
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++, mem += 8) {
1388c2ecf20Sopenharmony_ci			writew(r + ri * i, mem + 0);
1398c2ecf20Sopenharmony_ci			writew(g + gi * i, mem + 2);
1408c2ecf20Sopenharmony_ci			writew(b + bi * i, mem + 4);
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* INTERPOLATE modes require a "next" entry to interpolate with,
1458c2ecf20Sopenharmony_ci	 * so we replicate the last entry to deal with this for now.
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	writew(readw(mem - 8), mem + 0);
1488c2ecf20Sopenharmony_ci	writew(readw(mem - 6), mem + 2);
1498c2ecf20Sopenharmony_ci	writew(readw(mem - 4), mem + 4);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void
1538c2ecf20Sopenharmony_ciheadc57d_olut_load(struct drm_color_lut *in, int size, void __iomem *mem)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	memset_io(mem, 0x00, 0x20); /* VSS header. */
1568c2ecf20Sopenharmony_ci	mem += 0x20;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	for (; size--; in++, mem += 0x08) {
1598c2ecf20Sopenharmony_ci		writew(drm_color_lut_extract(in->  red, 16), mem + 0);
1608c2ecf20Sopenharmony_ci		writew(drm_color_lut_extract(in->green, 16), mem + 2);
1618c2ecf20Sopenharmony_ci		writew(drm_color_lut_extract(in-> blue, 16), mem + 4);
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* INTERPOLATE modes require a "next" entry to interpolate with,
1658c2ecf20Sopenharmony_ci	 * so we replicate the last entry to deal with this for now.
1668c2ecf20Sopenharmony_ci	 */
1678c2ecf20Sopenharmony_ci	writew(readw(mem - 8), mem + 0);
1688c2ecf20Sopenharmony_ci	writew(readw(mem - 6), mem + 2);
1698c2ecf20Sopenharmony_ci	writew(readw(mem - 4), mem + 4);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cibool
1738c2ecf20Sopenharmony_ciheadc57d_olut(struct nv50_head *head, struct nv50_head_atom *asyh, int size)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	if (size != 0 && size != 256 && size != 1024)
1768c2ecf20Sopenharmony_ci		return false;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	asyh->olut.mode = NVC57D_HEAD_SET_OLUT_CONTROL_MODE_DIRECT10;
1798c2ecf20Sopenharmony_ci	asyh->olut.size = 4 /* VSS header. */ + 1024 + 1 /* Entries. */;
1808c2ecf20Sopenharmony_ci	asyh->olut.output_mode = NVC57D_HEAD_SET_OLUT_CONTROL_INTERPOLATE_ENABLE;
1818c2ecf20Sopenharmony_ci	if (size == 256)
1828c2ecf20Sopenharmony_ci		asyh->olut.load = headc57d_olut_load_8;
1838c2ecf20Sopenharmony_ci	else
1848c2ecf20Sopenharmony_ci		asyh->olut.load = headc57d_olut_load;
1858c2ecf20Sopenharmony_ci	return true;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic int
1898c2ecf20Sopenharmony_ciheadc57d_mode(struct nv50_head *head, struct nv50_head_atom *asyh)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
1928c2ecf20Sopenharmony_ci	struct nv50_head_mode *m = &asyh->mode;
1938c2ecf20Sopenharmony_ci	const int i = head->base.index;
1948c2ecf20Sopenharmony_ci	int ret;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 15)))
1978c2ecf20Sopenharmony_ci		return ret;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_RASTER_SIZE(i),
2008c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SIZE, WIDTH, m->h.active) |
2018c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SIZE, HEIGHT, m->v.active),
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci				HEAD_SET_RASTER_SYNC_END(i),
2048c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SYNC_END, X, m->h.synce) |
2058c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_SYNC_END, Y, m->v.synce),
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci				HEAD_SET_RASTER_BLANK_END(i),
2088c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_END, X, m->h.blanke) |
2098c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_END, Y, m->v.blanke),
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci				HEAD_SET_RASTER_BLANK_START(i),
2128c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_START, X, m->h.blanks) |
2138c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_RASTER_BLANK_START, Y, m->v.blanks));
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	//XXX:
2168c2ecf20Sopenharmony_ci	PUSH_NVSQ(push, NVC57D, 0x2074 + (i * 0x400), m->v.blank2e << 16 | m->v.blank2s);
2178c2ecf20Sopenharmony_ci	PUSH_NVSQ(push, NVC57D, 0x2008 + (i * 0x400), m->interlace);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY(i),
2208c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY, HERTZ, m->clock * 1000));
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY_MAX(i),
2238c2ecf20Sopenharmony_ci		  NVVAL(NVC57D, HEAD_SET_PIXEL_CLOCK_FREQUENCY_MAX, HERTZ, m->clock * 1000));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/*XXX: HEAD_USAGE_BOUNDS, doesn't belong here. */
2268c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS(i),
2278c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, CURSOR, USAGE_W256_H256) |
2288c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, OLUT_ALLOWED, TRUE) |
2298c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, OUTPUT_SCALER_TAPS, TAPS_2) |
2308c2ecf20Sopenharmony_ci		  NVDEF(NVC57D, HEAD_SET_HEAD_USAGE_BOUNDS, UPSCALING_ALLOWED, TRUE));
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciconst struct nv50_head_func
2358c2ecf20Sopenharmony_ciheadc57d = {
2368c2ecf20Sopenharmony_ci	.view = headc37d_view,
2378c2ecf20Sopenharmony_ci	.mode = headc57d_mode,
2388c2ecf20Sopenharmony_ci	.olut = headc57d_olut,
2398c2ecf20Sopenharmony_ci	.olut_identity = true,
2408c2ecf20Sopenharmony_ci	.olut_size = 1024,
2418c2ecf20Sopenharmony_ci	.olut_set = headc57d_olut_set,
2428c2ecf20Sopenharmony_ci	.olut_clr = headc57d_olut_clr,
2438c2ecf20Sopenharmony_ci	.curs_layout = head917d_curs_layout,
2448c2ecf20Sopenharmony_ci	.curs_format = headc37d_curs_format,
2458c2ecf20Sopenharmony_ci	.curs_set = headc37d_curs_set,
2468c2ecf20Sopenharmony_ci	.curs_clr = headc37d_curs_clr,
2478c2ecf20Sopenharmony_ci	.dither = headc37d_dither,
2488c2ecf20Sopenharmony_ci	.procamp = headc57d_procamp,
2498c2ecf20Sopenharmony_ci	.or = headc57d_or,
2508c2ecf20Sopenharmony_ci	/* TODO: flexible window mappings */
2518c2ecf20Sopenharmony_ci	.static_wndw_map = headc37d_static_wndw_map,
2528c2ecf20Sopenharmony_ci};
253