summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nouveau/core/subdev/pwr/memx.c
blob: 65eaa2546cad2ce5abbcbc32eea9ac008c8bfd2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef __NVKM_PWR_MEMX_H__
#define __NVKM_PWR_MEMX_H__

#include "priv.h"

struct nouveau_memx {
	struct nouveau_pwr *ppwr;
	u32 base;
	u32 size;
	struct {
		u32 mthd;
		u32 size;
		u32 data[64];
	} c;
};

static void
memx_out(struct nouveau_memx *memx)
{
	struct nouveau_pwr *ppwr = memx->ppwr;
	int i;

	if (memx->c.mthd) {
		nv_wr32(ppwr, 0x10a1c4, (memx->c.size << 16) | memx->c.mthd);
		for (i = 0; i < memx->c.size; i++)
			nv_wr32(ppwr, 0x10a1c4, memx->c.data[i]);
		memx->c.mthd = 0;
		memx->c.size = 0;
	}
}

static void
memx_cmd(struct nouveau_memx *memx, u32 mthd, u32 size, u32 data[])
{
	if ((memx->c.size + size >= ARRAY_SIZE(memx->c.data)) ||
	    (memx->c.mthd && memx->c.mthd != mthd))
		memx_out(memx);
	memcpy(&memx->c.data[memx->c.size], data, size * sizeof(data[0]));
	memx->c.size += size;
	memx->c.mthd  = mthd;
}

int
nouveau_memx_init(struct nouveau_pwr *ppwr, struct nouveau_memx **pmemx)
{
	struct nouveau_memx *memx;
	u32 reply[2];
	int ret;

	ret = ppwr->message(ppwr, reply, PROC_MEMX, MEMX_MSG_INFO, 0, 0);
	if (ret)
		return ret;

	memx = *pmemx = kzalloc(sizeof(*memx), GFP_KERNEL);
	if (!memx)
		return -ENOMEM;
	memx->ppwr = ppwr;
	memx->base = reply[0];
	memx->size = reply[1];

	/* acquire data segment access */
	do {
		nv_wr32(ppwr, 0x10a580, 0x00000003);
	} while (nv_rd32(ppwr, 0x10a580) != 0x00000003);
	nv_wr32(ppwr, 0x10a1c0, 0x01000000 | memx->base);

	return 0;
}

int
nouveau_memx_fini(struct nouveau_memx **pmemx, bool exec)
{
	struct nouveau_memx *memx = *pmemx;
	struct nouveau_pwr *ppwr = memx->ppwr;
	u32 finish, reply[2];

	/* flush the cache... */
	memx_out(memx);

	/* release data segment access */
	finish = nv_rd32(ppwr, 0x10a1c0) & 0x00ffffff;
	nv_wr32(ppwr, 0x10a580, 0x00000000);

	/* call MEMX process to execute the script, and wait for reply */
	if (exec) {
		ppwr->message(ppwr, reply, PROC_MEMX, MEMX_MSG_EXEC,
				 memx->base, finish);
	}

	nv_debug(memx->ppwr, "Exec took %uns, PPWR_IN %08x\n",
		 reply[0], reply[1]);
	kfree(memx);
	return 0;
}

void
nouveau_memx_wr32(struct nouveau_memx *memx, u32 addr, u32 data)
{
	nv_debug(memx->ppwr, "R[%06x] = 0x%08x\n", addr, data);
	memx_cmd(memx, MEMX_WR32, 2, (u32[]){ addr, data });
}

void
nouveau_memx_wait(struct nouveau_memx *memx,
		  u32 addr, u32 mask, u32 data, u32 nsec)
{
	nv_debug(memx->ppwr, "R[%06x] & 0x%08x == 0x%08x, %d us\n",
				addr, mask, data, nsec);
	memx_cmd(memx, MEMX_WAIT, 4, (u32[]){ addr, ~mask, data, nsec });
	memx_out(memx); /* fuc can't handle multiple */
}

void
nouveau_memx_nsec(struct nouveau_memx *memx, u32 nsec)
{
	nv_debug(memx->ppwr, "    DELAY = %d ns\n", nsec);
	memx_cmd(memx, MEMX_DELAY, 1, (u32[]){ nsec });
	memx_out(memx); /* fuc can't handle multiple */
}

void
nouveau_memx_wait_vblank(struct nouveau_memx *memx)
{
	struct nouveau_pwr *ppwr = memx->ppwr;
	u32 heads, x, y, px = 0;
	int i, head_sync;

	if (nv_device(ppwr)->chipset < 0xd0) {
		heads = nv_rd32(ppwr, 0x610050);
		for (i = 0; i < 2; i++) {
			/* Heuristic: sync to head with biggest resolution */
			if (heads & (2 << (i << 3))) {
				x = nv_rd32(ppwr, 0x610b40 + (0x540 * i));
				y = (x & 0xffff0000) >> 16;
				x &= 0x0000ffff;
				if ((x * y) > px) {
					px = (x * y);
					head_sync = i;
				}
			}
		}
	}

	if (px == 0) {
		nv_debug(memx->ppwr, "WAIT VBLANK !NO ACTIVE HEAD\n");
		return;
	}

	nv_debug(memx->ppwr, "WAIT VBLANK HEAD%d\n", head_sync);
	memx_cmd(memx, MEMX_VBLANK, 1, (u32[]){ head_sync });
	memx_out(memx); /* fuc can't handle multiple */
}

void
nouveau_memx_block(struct nouveau_memx *memx)
{
	nv_debug(memx->ppwr, "   HOST BLOCKED\n");
	memx_cmd(memx, MEMX_ENTER, 0, NULL);
}

void
nouveau_memx_unblock(struct nouveau_memx *memx)
{
	nv_debug(memx->ppwr, "   HOST UNBLOCKED\n");
	memx_cmd(memx, MEMX_LEAVE, 0, NULL);
}

#endif