162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2018 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#include "wndw.h"
2362306a36Sopenharmony_ci#include "atom.h"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <drm/drm_atomic_helper.h>
2662306a36Sopenharmony_ci#include <nouveau_bo.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <nvif/pushc37b.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include <nvhw/class/clc57e.h>
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic int
3362306a36Sopenharmony_ciwndwc57e_image_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
3662306a36Sopenharmony_ci	int ret;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 17)))
3962306a36Sopenharmony_ci		return ret;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_PRESENT_CONTROL,
4262306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_PRESENT_CONTROL, MIN_PRESENT_INTERVAL, asyw->image.interval) |
4362306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_PRESENT_CONTROL, BEGIN_MODE, asyw->image.mode) |
4462306a36Sopenharmony_ci		  NVDEF(NVC57E, SET_PRESENT_CONTROL, TIMESTAMP_MODE, DISABLE));
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_SIZE,
4762306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE, WIDTH, asyw->image.w) |
4862306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE, HEIGHT, asyw->image.h),
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci				SET_STORAGE,
5162306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_STORAGE, BLOCK_HEIGHT, asyw->image.blockh) |
5262306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_STORAGE, MEMORY_LAYOUT, asyw->image.layout),
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci				SET_PARAMS,
5562306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_PARAMS, FORMAT, asyw->image.format) |
5662306a36Sopenharmony_ci		  NVDEF(NVC57E, SET_PARAMS, CLAMP_BEFORE_BLEND, DISABLE) |
5762306a36Sopenharmony_ci		  NVDEF(NVC57E, SET_PARAMS, SWAP_UV, DISABLE) |
5862306a36Sopenharmony_ci		  NVDEF(NVC57E, SET_PARAMS, FMT_ROUNDING_MODE, ROUND_TO_NEAREST),
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci				SET_PLANAR_STORAGE(0),
6162306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_PLANAR_STORAGE, PITCH, asyw->image.blocks[0]) |
6262306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_PLANAR_STORAGE, PITCH, asyw->image.pitch[0] >> 6));
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_CONTEXT_DMA_ISO(0), asyw->image.handle, 1);
6562306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_OFFSET(0), asyw->image.offset[0] >> 8);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_POINT_IN(0),
6862306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_POINT_IN, X, asyw->state.src_x >> 16) |
6962306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_POINT_IN, Y, asyw->state.src_y >> 16));
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_SIZE_IN,
7262306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE_IN, WIDTH, asyw->state.src_w >> 16) |
7362306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE_IN, HEIGHT, asyw->state.src_h >> 16));
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_SIZE_OUT,
7662306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE_OUT, WIDTH, asyw->state.crtc_w) |
7762306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_SIZE_OUT, HEIGHT, asyw->state.crtc_h));
7862306a36Sopenharmony_ci	return 0;
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciint
8262306a36Sopenharmony_ciwndwc57e_csc_clr(struct nv50_wndw *wndw)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
8562306a36Sopenharmony_ci	const u32 identity[12] = {
8662306a36Sopenharmony_ci		0x00010000, 0x00000000, 0x00000000, 0x00000000,
8762306a36Sopenharmony_ci		0x00000000, 0x00010000, 0x00000000, 0x00000000,
8862306a36Sopenharmony_ci		0x00000000, 0x00000000, 0x00010000, 0x00000000,
8962306a36Sopenharmony_ci	};
9062306a36Sopenharmony_ci	int ret;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 1 + ARRAY_SIZE(identity))))
9362306a36Sopenharmony_ci		return ret;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_FMT_COEFFICIENT_C00, identity, ARRAY_SIZE(identity));
9662306a36Sopenharmony_ci	return 0;
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciint
10062306a36Sopenharmony_ciwndwc57e_csc_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
10362306a36Sopenharmony_ci	int ret;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 13)))
10662306a36Sopenharmony_ci		return ret;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_FMT_COEFFICIENT_C00, asyw->csc.matrix, 12);
10962306a36Sopenharmony_ci	return 0;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciint
11362306a36Sopenharmony_ciwndwc57e_ilut_clr(struct nv50_wndw *wndw)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
11662306a36Sopenharmony_ci	int ret;
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 2)))
11962306a36Sopenharmony_ci		return ret;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_CONTEXT_DMA_ILUT, 0x00000000);
12262306a36Sopenharmony_ci	return 0;
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciint
12662306a36Sopenharmony_ciwndwc57e_ilut_set(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	struct nvif_push *push = wndw->wndw.push;
12962306a36Sopenharmony_ci	int ret;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	if ((ret = PUSH_WAIT(push, 4)))
13262306a36Sopenharmony_ci		return ret;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	PUSH_MTHD(push, NVC57E, SET_ILUT_CONTROL,
13562306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_ILUT_CONTROL, SIZE, asyw->xlut.i.size) |
13662306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_ILUT_CONTROL, MODE, asyw->xlut.i.mode) |
13762306a36Sopenharmony_ci		  NVVAL(NVC57E, SET_ILUT_CONTROL, INTERPOLATE, asyw->xlut.i.output_mode),
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci				SET_CONTEXT_DMA_ILUT, asyw->xlut.handle,
14062306a36Sopenharmony_ci				SET_OFFSET_ILUT, asyw->xlut.i.offset >> 8);
14162306a36Sopenharmony_ci	return 0;
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic u16
14562306a36Sopenharmony_cifixedU0_16_FP16(u16 fixed)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci        int sign = 0, exp = 0, man = 0;
14862306a36Sopenharmony_ci        if (fixed) {
14962306a36Sopenharmony_ci                while (--exp && !(fixed & 0x8000))
15062306a36Sopenharmony_ci                        fixed <<= 1;
15162306a36Sopenharmony_ci                man = ((fixed << 1) & 0xffc0) >> 6;
15262306a36Sopenharmony_ci                exp += 15;
15362306a36Sopenharmony_ci        }
15462306a36Sopenharmony_ci        return (sign << 15) | (exp << 10) | man;
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistatic void
15862306a36Sopenharmony_ciwndwc57e_ilut_load(struct drm_color_lut *in, int size, void __iomem *mem)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	memset_io(mem, 0x00, 0x20); /* VSS header. */
16162306a36Sopenharmony_ci	mem += 0x20;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	for (; size--; in++, mem += 0x08) {
16462306a36Sopenharmony_ci		u16 r = fixedU0_16_FP16(drm_color_lut_extract(in->  red, 16));
16562306a36Sopenharmony_ci		u16 g = fixedU0_16_FP16(drm_color_lut_extract(in->green, 16));
16662306a36Sopenharmony_ci		u16 b = fixedU0_16_FP16(drm_color_lut_extract(in-> blue, 16));
16762306a36Sopenharmony_ci		writew(r, mem + 0);
16862306a36Sopenharmony_ci		writew(g, mem + 2);
16962306a36Sopenharmony_ci		writew(b, mem + 4);
17062306a36Sopenharmony_ci	}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	/* INTERPOLATE modes require a "next" entry to interpolate with,
17362306a36Sopenharmony_ci	 * so we replicate the last entry to deal with this for now.
17462306a36Sopenharmony_ci	 */
17562306a36Sopenharmony_ci	writew(readw(mem - 8), mem + 0);
17662306a36Sopenharmony_ci	writew(readw(mem - 6), mem + 2);
17762306a36Sopenharmony_ci	writew(readw(mem - 4), mem + 4);
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_civoid
18162306a36Sopenharmony_ciwndwc57e_ilut(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw, int size)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	if (!size)
18462306a36Sopenharmony_ci		size = 1024;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	if (size == 256)
18762306a36Sopenharmony_ci		asyw->xlut.i.mode = NVC57E_SET_ILUT_CONTROL_MODE_DIRECT8;
18862306a36Sopenharmony_ci	else
18962306a36Sopenharmony_ci		asyw->xlut.i.mode = NVC57E_SET_ILUT_CONTROL_MODE_DIRECT10;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	asyw->xlut.i.size = 4 /* VSS header. */ + size + 1 /* Entries. */;
19262306a36Sopenharmony_ci	asyw->xlut.i.output_mode = NVC57E_SET_ILUT_CONTROL_INTERPOLATE_DISABLE;
19362306a36Sopenharmony_ci	asyw->xlut.i.load = wndwc57e_ilut_load;
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/****************************************************************
19762306a36Sopenharmony_ci *            Log2(block height) ----------------------------+  *
19862306a36Sopenharmony_ci *            Page Kind ----------------------------------+  |  *
19962306a36Sopenharmony_ci *            Gob Height/Page Kind Generation ------+     |  |  *
20062306a36Sopenharmony_ci *                          Sector layout -------+  |     |  |  *
20162306a36Sopenharmony_ci *                          Compression ------+  |  |     |  |  */
20262306a36Sopenharmony_ciconst u64 wndwc57e_modifiers[] = { /*         |  |  |     |  |  */
20362306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 0),
20462306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 1),
20562306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 2),
20662306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 3),
20762306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 4),
20862306a36Sopenharmony_ci	DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, 1, 2, 0x06, 5),
20962306a36Sopenharmony_ci	DRM_FORMAT_MOD_LINEAR,
21062306a36Sopenharmony_ci	DRM_FORMAT_MOD_INVALID
21162306a36Sopenharmony_ci};
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic const struct nv50_wndw_func
21462306a36Sopenharmony_ciwndwc57e = {
21562306a36Sopenharmony_ci	.acquire = wndwc37e_acquire,
21662306a36Sopenharmony_ci	.release = wndwc37e_release,
21762306a36Sopenharmony_ci	.sema_set = wndwc37e_sema_set,
21862306a36Sopenharmony_ci	.sema_clr = wndwc37e_sema_clr,
21962306a36Sopenharmony_ci	.ntfy_set = wndwc37e_ntfy_set,
22062306a36Sopenharmony_ci	.ntfy_clr = wndwc37e_ntfy_clr,
22162306a36Sopenharmony_ci	.ntfy_reset = corec37d_ntfy_init,
22262306a36Sopenharmony_ci	.ntfy_wait_begun = base507c_ntfy_wait_begun,
22362306a36Sopenharmony_ci	.ilut = wndwc57e_ilut,
22462306a36Sopenharmony_ci	.ilut_identity = true,
22562306a36Sopenharmony_ci	.ilut_size = 1024,
22662306a36Sopenharmony_ci	.xlut_set = wndwc57e_ilut_set,
22762306a36Sopenharmony_ci	.xlut_clr = wndwc57e_ilut_clr,
22862306a36Sopenharmony_ci	.csc = base907c_csc,
22962306a36Sopenharmony_ci	.csc_set = wndwc57e_csc_set,
23062306a36Sopenharmony_ci	.csc_clr = wndwc57e_csc_clr,
23162306a36Sopenharmony_ci	.image_set = wndwc57e_image_set,
23262306a36Sopenharmony_ci	.image_clr = wndwc37e_image_clr,
23362306a36Sopenharmony_ci	.blend_set = wndwc37e_blend_set,
23462306a36Sopenharmony_ci	.update = wndwc37e_update,
23562306a36Sopenharmony_ci};
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ciint
23862306a36Sopenharmony_ciwndwc57e_new(struct nouveau_drm *drm, enum drm_plane_type type, int index,
23962306a36Sopenharmony_ci	     s32 oclass, struct nv50_wndw **pwndw)
24062306a36Sopenharmony_ci{
24162306a36Sopenharmony_ci	return wndwc37e_new_(&wndwc57e, drm, type, index, oclass,
24262306a36Sopenharmony_ci			     BIT(index >> 1), pwndw);
24362306a36Sopenharmony_ci}
244