112885ecbSLyude Paul // SPDX-License-Identifier: MIT
212885ecbSLyude Paul #include <drm/drm_crtc.h>
312885ecbSLyude Paul 
412885ecbSLyude Paul #include "crc.h"
5*57cbdbe6SLyude Paul #include "crcc37d.h"
612885ecbSLyude Paul #include "core.h"
712885ecbSLyude Paul #include "disp.h"
812885ecbSLyude Paul #include "head.h"
912885ecbSLyude Paul 
10a255e9c8SLyude Paul #include <nvif/pushc37b.h>
11c4b27bc8SBen Skeggs 
126162638eSBen Skeggs #include <nvhw/class/clc37d.h>
136162638eSBen Skeggs 
14c4b27bc8SBen Skeggs static int
crcc37d_set_src(struct nv50_head * head,int or,enum nv50_crc_source_type source,struct nv50_crc_notifier_ctx * ctx)15*57cbdbe6SLyude Paul crcc37d_set_src(struct nv50_head *head, int or, enum nv50_crc_source_type source,
16*57cbdbe6SLyude Paul 		struct nv50_crc_notifier_ctx *ctx)
1712885ecbSLyude Paul {
18c4b27bc8SBen Skeggs 	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
19c4b27bc8SBen Skeggs 	const int i = head->base.index;
20*57cbdbe6SLyude Paul 	u32 crc_args = NVVAL(NVC37D, HEAD_SET_CRC_CONTROL, CONTROLLING_CHANNEL, i * 4) |
216162638eSBen Skeggs 		       NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, EXPECT_BUFFER_COLLAPSE, FALSE) |
226162638eSBen Skeggs 		       NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, SECONDARY_CRC, NONE) |
236162638eSBen Skeggs 		       NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, CRC_DURING_SNOOZE, DISABLE);
24c4b27bc8SBen Skeggs 	int ret;
2512885ecbSLyude Paul 
2612885ecbSLyude Paul 	switch (source) {
2712885ecbSLyude Paul 	case NV50_CRC_SOURCE_TYPE_SOR:
286162638eSBen Skeggs 		crc_args |= NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, PRIMARY_CRC, SOR(or));
2912885ecbSLyude Paul 		break;
3012885ecbSLyude Paul 	case NV50_CRC_SOURCE_TYPE_PIOR:
316162638eSBen Skeggs 		crc_args |= NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, PRIMARY_CRC, PIOR(or));
3212885ecbSLyude Paul 		break;
3312885ecbSLyude Paul 	case NV50_CRC_SOURCE_TYPE_SF:
346162638eSBen Skeggs 		crc_args |= NVDEF(NVC37D, HEAD_SET_CRC_CONTROL, PRIMARY_CRC, SF);
3512885ecbSLyude Paul 		break;
3612885ecbSLyude Paul 	default:
3712885ecbSLyude Paul 		break;
3812885ecbSLyude Paul 	}
3912885ecbSLyude Paul 
40c4b27bc8SBen Skeggs 	if ((ret = PUSH_WAIT(push, 4)))
41c4b27bc8SBen Skeggs 		return ret;
4212885ecbSLyude Paul 
4312885ecbSLyude Paul 	if (source) {
446162638eSBen Skeggs 		PUSH_MTHD(push, NVC37D, HEAD_SET_CONTEXT_DMA_CRC(i), ctx->ntfy.handle);
456162638eSBen Skeggs 		PUSH_MTHD(push, NVC37D, HEAD_SET_CRC_CONTROL(i), crc_args);
4612885ecbSLyude Paul 	} else {
476162638eSBen Skeggs 		PUSH_MTHD(push, NVC37D, HEAD_SET_CRC_CONTROL(i), 0);
486162638eSBen Skeggs 		PUSH_MTHD(push, NVC37D, HEAD_SET_CONTEXT_DMA_CRC(i), 0);
4912885ecbSLyude Paul 	}
5012885ecbSLyude Paul 
51c4b27bc8SBen Skeggs 	return 0;
5212885ecbSLyude Paul }
5312885ecbSLyude Paul 
crcc37d_set_ctx(struct nv50_head * head,struct nv50_crc_notifier_ctx * ctx)54*57cbdbe6SLyude Paul int crcc37d_set_ctx(struct nv50_head *head, struct nv50_crc_notifier_ctx *ctx)
5512885ecbSLyude Paul {
56ae09163aSBen Skeggs 	struct nvif_push *push = nv50_disp(head->base.base.dev)->core->chan.push;
57ae09163aSBen Skeggs 	const int i = head->base.index;
58ae09163aSBen Skeggs 	int ret;
5912885ecbSLyude Paul 
60ae09163aSBen Skeggs 	if ((ret = PUSH_WAIT(push, 2)))
61ae09163aSBen Skeggs 		return ret;
6212885ecbSLyude Paul 
631fad04ceSBen Skeggs 	PUSH_MTHD(push, NVC37D, HEAD_SET_CONTEXT_DMA_CRC(i), ctx ? ctx->ntfy.handle : 0);
64ae09163aSBen Skeggs 	return 0;
6512885ecbSLyude Paul }
6612885ecbSLyude Paul 
crcc37d_get_entry(struct nv50_head * head,struct nv50_crc_notifier_ctx * ctx,enum nv50_crc_source source,int idx)67*57cbdbe6SLyude Paul u32 crcc37d_get_entry(struct nv50_head *head, struct nv50_crc_notifier_ctx *ctx,
6812885ecbSLyude Paul 		      enum nv50_crc_source source, int idx)
6912885ecbSLyude Paul {
7012885ecbSLyude Paul 	struct crcc37d_notifier __iomem *notifier = ctx->mem.object.map.ptr;
7112885ecbSLyude Paul 	struct crcc37d_entry __iomem *entry = &notifier->entries[idx];
7212885ecbSLyude Paul 	u32 __iomem *crc_addr;
7312885ecbSLyude Paul 
7412885ecbSLyude Paul 	if (source == NV50_CRC_SOURCE_RG)
7512885ecbSLyude Paul 		crc_addr = &entry->rg_crc;
7612885ecbSLyude Paul 	else
7712885ecbSLyude Paul 		crc_addr = &entry->output_crc[0];
7812885ecbSLyude Paul 
7912885ecbSLyude Paul 	return ioread32_native(crc_addr);
8012885ecbSLyude Paul }
8112885ecbSLyude Paul 
crcc37d_ctx_finished(struct nv50_head * head,struct nv50_crc_notifier_ctx * ctx)82*57cbdbe6SLyude Paul bool crcc37d_ctx_finished(struct nv50_head *head, struct nv50_crc_notifier_ctx *ctx)
8312885ecbSLyude Paul {
8412885ecbSLyude Paul 	struct nouveau_drm *drm = nouveau_drm(head->base.base.dev);
8512885ecbSLyude Paul 	struct crcc37d_notifier __iomem *notifier = ctx->mem.object.map.ptr;
8612885ecbSLyude Paul 	const u32 status = ioread32_native(&notifier->status);
8712885ecbSLyude Paul 	const u32 overflow = status & 0x0000007e;
8812885ecbSLyude Paul 
8912885ecbSLyude Paul 	if (!(status & 0x00000001))
9012885ecbSLyude Paul 		return false;
9112885ecbSLyude Paul 
9212885ecbSLyude Paul 	if (overflow) {
9312885ecbSLyude Paul 		const char *engine = NULL;
9412885ecbSLyude Paul 
9512885ecbSLyude Paul 		switch (overflow) {
9612885ecbSLyude Paul 		case 0x00000004: engine = "Front End"; break;
9712885ecbSLyude Paul 		case 0x00000008: engine = "Compositor"; break;
9812885ecbSLyude Paul 		case 0x00000010: engine = "RG"; break;
9912885ecbSLyude Paul 		case 0x00000020: engine = "CRC output 1"; break;
10012885ecbSLyude Paul 		case 0x00000040: engine = "CRC output 2"; break;
10112885ecbSLyude Paul 		}
10212885ecbSLyude Paul 
10312885ecbSLyude Paul 		if (engine)
10412885ecbSLyude Paul 			NV_ERROR(drm,
10512885ecbSLyude Paul 				 "CRC notifier context for head %d overflowed on %s: %x\n",
10612885ecbSLyude Paul 				 head->base.index, engine, status);
10712885ecbSLyude Paul 		else
10812885ecbSLyude Paul 			NV_ERROR(drm,
10912885ecbSLyude Paul 				 "CRC notifier context for head %d overflowed: %x\n",
11012885ecbSLyude Paul 				 head->base.index, status);
11112885ecbSLyude Paul 	}
11212885ecbSLyude Paul 
11312885ecbSLyude Paul 	NV_DEBUG(drm, "Head %d CRC context status: %x\n",
11412885ecbSLyude Paul 		 head->base.index, status);
11512885ecbSLyude Paul 
11612885ecbSLyude Paul 	return true;
11712885ecbSLyude Paul }
11812885ecbSLyude Paul 
11912885ecbSLyude Paul const struct nv50_crc_func crcc37d = {
12012885ecbSLyude Paul 	.set_src = crcc37d_set_src,
12112885ecbSLyude Paul 	.set_ctx = crcc37d_set_ctx,
12212885ecbSLyude Paul 	.get_entry = crcc37d_get_entry,
12312885ecbSLyude Paul 	.ctx_finished = crcc37d_ctx_finished,
124*57cbdbe6SLyude Paul 	.flip_threshold = CRCC37D_FLIP_THRESHOLD,
12512885ecbSLyude Paul 	.num_entries = CRCC37D_MAX_ENTRIES,
12612885ecbSLyude Paul 	.notifier_len = sizeof(struct crcc37d_notifier),
12712885ecbSLyude Paul };
128