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 "base.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <nvif/push507c.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <nvhw/class/cl907c.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int
298c2ecf20Sopenharmony_cibase907c_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
328c2ecf20Sopenharmony_ci	int ret;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 10)))
358c2ecf20Sopenharmony_ci		return ret;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_PRESENT_CONTROL,
388c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SET_PRESENT_CONTROL, BEGIN_MODE, asyw->image.mode) |
398c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_PRESENT_CONTROL, TIMESTAMP_MODE, DISABLE) |
408c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SET_PRESENT_CONTROL, MIN_PRESENT_INTERVAL, asyw->image.interval));
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_CONTEXT_DMAS_ISO(0), asyw->image.handle, 1);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SURFACE_SET_OFFSET(0, 0), asyw->image.offset[0] >> 8,
458c2ecf20Sopenharmony_ci				SURFACE_SET_OFFSET(0, 1), 0x00000000,
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci				SURFACE_SET_SIZE(0),
488c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_SIZE, WIDTH, asyw->image.w) |
498c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_SIZE, HEIGHT, asyw->image.h),
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci				SURFACE_SET_STORAGE(0),
528c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_STORAGE, BLOCK_HEIGHT, asyw->image.blockh) |
538c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_STORAGE, PITCH, asyw->image.pitch[0] >> 8) |
548c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_STORAGE, PITCH, asyw->image.blocks[0]) |
558c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_STORAGE, MEMORY_LAYOUT, asyw->image.layout),
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci				SURFACE_SET_PARAMS(0),
588c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SURFACE_SET_PARAMS, FORMAT, asyw->image.format) |
598c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SURFACE_SET_PARAMS, SUPER_SAMPLE, X1_AA) |
608c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SURFACE_SET_PARAMS, GAMMA, LINEAR) |
618c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SURFACE_SET_PARAMS, LAYOUT, FRM));
628c2ecf20Sopenharmony_ci	return 0;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int
668c2ecf20Sopenharmony_cibase907c_xlut_clr(struct nv50_wndw *wndw)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 6)))
728c2ecf20Sopenharmony_ci		return ret;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_BASE_LUT_LO,
758c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_BASE_LUT_LO, ENABLE, DISABLE));
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_OUTPUT_LUT_LO,
788c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_OUTPUT_LUT_LO, ENABLE, DISABLE));
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_CONTEXT_DMA_LUT, 0x00000000);
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int
858c2ecf20Sopenharmony_cibase907c_xlut_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
888c2ecf20Sopenharmony_ci	int ret;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 6)))
918c2ecf20Sopenharmony_ci		return ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_BASE_LUT_LO,
948c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SET_BASE_LUT_LO, ENABLE, asyw->xlut.i.enable) |
958c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SET_BASE_LUT_LO, MODE, asyw->xlut.i.mode),
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci				SET_BASE_LUT_HI, asyw->xlut.i.offset >> 8,
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci				SET_OUTPUT_LUT_LO,
1008c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_OUTPUT_LUT_LO, ENABLE, USE_CORE_LUT));
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_CONTEXT_DMA_LUT, asyw->xlut.handle);
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic bool
1078c2ecf20Sopenharmony_cibase907c_ilut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw, int size)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	if (size != 256 && size != 1024)
1108c2ecf20Sopenharmony_ci		return false;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (size == 1024)
1138c2ecf20Sopenharmony_ci		asyw->xlut.i.mode = NV907C_SET_BASE_LUT_LO_MODE_INTERPOLATE_1025_UNITY_RANGE;
1148c2ecf20Sopenharmony_ci	else
1158c2ecf20Sopenharmony_ci		asyw->xlut.i.mode = NV907C_SET_BASE_LUT_LO_MODE_INTERPOLATE_257_UNITY_RANGE;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	asyw->xlut.i.enable = NV907C_SET_BASE_LUT_LO_ENABLE_ENABLE;
1188c2ecf20Sopenharmony_ci	asyw->xlut.i.load = head907d_olut_load;
1198c2ecf20Sopenharmony_ci	return true;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline u32
1238c2ecf20Sopenharmony_cicsc_drm_to_base(u64 in)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	/* base takes a 19-bit 2's complement value in S3.16 format */
1268c2ecf20Sopenharmony_ci	bool sign = in & BIT_ULL(63);
1278c2ecf20Sopenharmony_ci	u32 integer = (in >> 32) & 0x7fffffff;
1288c2ecf20Sopenharmony_ci	u32 fraction = in & 0xffffffff;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (integer >= 4) {
1318c2ecf20Sopenharmony_ci		return (1 << 18) - (sign ? 0 : 1);
1328c2ecf20Sopenharmony_ci	} else {
1338c2ecf20Sopenharmony_ci		u32 ret = (integer << 16) | (fraction >> 16);
1348c2ecf20Sopenharmony_ci		if (sign)
1358c2ecf20Sopenharmony_ci			ret = -ret;
1368c2ecf20Sopenharmony_ci		return ret & GENMASK(18, 0);
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_civoid
1418c2ecf20Sopenharmony_cibase907c_csc(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
1428c2ecf20Sopenharmony_ci	     const struct drm_color_ctm *ctm)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	int i, j;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	for (j = 0; j < 3; j++) {
1478c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++) {
1488c2ecf20Sopenharmony_ci			u32 *val = &asyw->csc.matrix[j * 4 + i];
1498c2ecf20Sopenharmony_ci			/* DRM does not support constant offset, while
1508c2ecf20Sopenharmony_ci			 * HW CSC does. Skip it. */
1518c2ecf20Sopenharmony_ci			if (i == 3) {
1528c2ecf20Sopenharmony_ci				*val = 0;
1538c2ecf20Sopenharmony_ci			} else {
1548c2ecf20Sopenharmony_ci				*val = csc_drm_to_base(ctm->matrix[j * 3 + i]);
1558c2ecf20Sopenharmony_ci			}
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int
1618c2ecf20Sopenharmony_cibase907c_csc_clr(struct nv50_wndw *wndw)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
1648c2ecf20Sopenharmony_ci	int ret;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
1678c2ecf20Sopenharmony_ci		return ret;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_CSC_RED2RED,
1708c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_CSC_RED2RED, OWNER, CORE));
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int
1758c2ecf20Sopenharmony_cibase907c_csc_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
1788c2ecf20Sopenharmony_ci	int ret;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 13)))
1818c2ecf20Sopenharmony_ci		return ret;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	PUSH_MTHD(push, NV907C, SET_CSC_RED2RED,
1848c2ecf20Sopenharmony_ci		  NVDEF(NV907C, SET_CSC_RED2RED, OWNER, BASE) |
1858c2ecf20Sopenharmony_ci		  NVVAL(NV907C, SET_CSC_RED2RED, COEFF, asyw->csc.matrix[0]),
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci				SET_CSC_GRN2RED, &asyw->csc.matrix[1], 11);
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciconst struct nv50_wndw_func
1928c2ecf20Sopenharmony_cibase907c = {
1938c2ecf20Sopenharmony_ci	.acquire = base507c_acquire,
1948c2ecf20Sopenharmony_ci	.release = base507c_release,
1958c2ecf20Sopenharmony_ci	.sema_set = base507c_sema_set,
1968c2ecf20Sopenharmony_ci	.sema_clr = base507c_sema_clr,
1978c2ecf20Sopenharmony_ci	.ntfy_reset = base507c_ntfy_reset,
1988c2ecf20Sopenharmony_ci	.ntfy_set = base507c_ntfy_set,
1998c2ecf20Sopenharmony_ci	.ntfy_clr = base507c_ntfy_clr,
2008c2ecf20Sopenharmony_ci	.ntfy_wait_begun = base507c_ntfy_wait_begun,
2018c2ecf20Sopenharmony_ci	.ilut = base907c_ilut,
2028c2ecf20Sopenharmony_ci	.csc = base907c_csc,
2038c2ecf20Sopenharmony_ci	.csc_set = base907c_csc_set,
2048c2ecf20Sopenharmony_ci	.csc_clr = base907c_csc_clr,
2058c2ecf20Sopenharmony_ci	.olut_core = true,
2068c2ecf20Sopenharmony_ci	.ilut_size = 1024,
2078c2ecf20Sopenharmony_ci	.xlut_set = base907c_xlut_set,
2088c2ecf20Sopenharmony_ci	.xlut_clr = base907c_xlut_clr,
2098c2ecf20Sopenharmony_ci	.image_set = base907c_image_set,
2108c2ecf20Sopenharmony_ci	.image_clr = base507c_image_clr,
2118c2ecf20Sopenharmony_ci	.update = base507c_update,
2128c2ecf20Sopenharmony_ci};
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciint
2158c2ecf20Sopenharmony_cibase907c_new(struct nouveau_drm *drm, int head, s32 oclass,
2168c2ecf20Sopenharmony_ci	     struct nv50_wndw **pwndw)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	return base507c_new_(&base907c, base507c_format, drm, head, oclass,
2198c2ecf20Sopenharmony_ci			     0x00000002 << (head * 4), pwndw);
2208c2ecf20Sopenharmony_ci}
221