1 #ifndef __NVKM_PWR_MEMX_H__
2 #define __NVKM_PWR_MEMX_H__
3
4 #include "priv.h"
5
6 struct nouveau_memx {
7 struct nouveau_pwr *ppwr;
8 u32 base;
9 u32 size;
10 struct {
11 u32 mthd;
12 u32 size;
13 u32 data[64];
14 } c;
15 };
16
17 static void
memx_out(struct nouveau_memx * memx)18 memx_out(struct nouveau_memx *memx)
19 {
20 struct nouveau_pwr *ppwr = memx->ppwr;
21 int i;
22
23 if (memx->c.mthd) {
24 nv_wr32(ppwr, 0x10a1c4, (memx->c.size << 16) | memx->c.mthd);
25 for (i = 0; i < memx->c.size; i++)
26 nv_wr32(ppwr, 0x10a1c4, memx->c.data[i]);
27 memx->c.mthd = 0;
28 memx->c.size = 0;
29 }
30 }
31
32 static void
memx_cmd(struct nouveau_memx * memx,u32 mthd,u32 size,u32 data[])33 memx_cmd(struct nouveau_memx *memx, u32 mthd, u32 size, u32 data[])
34 {
35 if ((memx->c.size + size >= ARRAY_SIZE(memx->c.data)) ||
36 (memx->c.mthd && memx->c.mthd != mthd))
37 memx_out(memx);
38 memcpy(&memx->c.data[memx->c.size], data, size * sizeof(data[0]));
39 memx->c.size += size;
40 memx->c.mthd = mthd;
41 }
42
43 int
nouveau_memx_init(struct nouveau_pwr * ppwr,struct nouveau_memx ** pmemx)44 nouveau_memx_init(struct nouveau_pwr *ppwr, struct nouveau_memx **pmemx)
45 {
46 struct nouveau_memx *memx;
47 u32 reply[2];
48 int ret;
49
50 ret = ppwr->message(ppwr, reply, PROC_MEMX, MEMX_MSG_INFO, 0, 0);
51 if (ret)
52 return ret;
53
54 memx = *pmemx = kzalloc(sizeof(*memx), GFP_KERNEL);
55 if (!memx)
56 return -ENOMEM;
57 memx->ppwr = ppwr;
58 memx->base = reply[0];
59 memx->size = reply[1];
60
61 /* acquire data segment access */
62 do {
63 nv_wr32(ppwr, 0x10a580, 0x00000003);
64 } while (nv_rd32(ppwr, 0x10a580) != 0x00000003);
65 nv_wr32(ppwr, 0x10a1c0, 0x01000000 | memx->base);
66
67 return 0;
68 }
69
70 int
nouveau_memx_fini(struct nouveau_memx ** pmemx,bool exec)71 nouveau_memx_fini(struct nouveau_memx **pmemx, bool exec)
72 {
73 struct nouveau_memx *memx = *pmemx;
74 struct nouveau_pwr *ppwr = memx->ppwr;
75 u32 finish, reply[2];
76
77 /* flush the cache... */
78 memx_out(memx);
79
80 /* release data segment access */
81 finish = nv_rd32(ppwr, 0x10a1c0) & 0x00ffffff;
82 nv_wr32(ppwr, 0x10a580, 0x00000000);
83
84 /* call MEMX process to execute the script, and wait for reply */
85 if (exec) {
86 ppwr->message(ppwr, reply, PROC_MEMX, MEMX_MSG_EXEC,
87 memx->base, finish);
88 }
89
90 nv_debug(memx->ppwr, "Exec took %uns, PPWR_IN %08x\n",
91 reply[0], reply[1]);
92 kfree(memx);
93 return 0;
94 }
95
96 void
nouveau_memx_wr32(struct nouveau_memx * memx,u32 addr,u32 data)97 nouveau_memx_wr32(struct nouveau_memx *memx, u32 addr, u32 data)
98 {
99 nv_debug(memx->ppwr, "R[%06x] = 0x%08x\n", addr, data);
100 memx_cmd(memx, MEMX_WR32, 2, (u32[]){ addr, data });
101 }
102
103 void
nouveau_memx_wait(struct nouveau_memx * memx,u32 addr,u32 mask,u32 data,u32 nsec)104 nouveau_memx_wait(struct nouveau_memx *memx,
105 u32 addr, u32 mask, u32 data, u32 nsec)
106 {
107 nv_debug(memx->ppwr, "R[%06x] & 0x%08x == 0x%08x, %d us\n",
108 addr, mask, data, nsec);
109 memx_cmd(memx, MEMX_WAIT, 4, (u32[]){ addr, ~mask, data, nsec });
110 memx_out(memx); /* fuc can't handle multiple */
111 }
112
113 void
nouveau_memx_nsec(struct nouveau_memx * memx,u32 nsec)114 nouveau_memx_nsec(struct nouveau_memx *memx, u32 nsec)
115 {
116 nv_debug(memx->ppwr, " DELAY = %d ns\n", nsec);
117 memx_cmd(memx, MEMX_DELAY, 1, (u32[]){ nsec });
118 memx_out(memx); /* fuc can't handle multiple */
119 }
120
121 void
nouveau_memx_wait_vblank(struct nouveau_memx * memx)122 nouveau_memx_wait_vblank(struct nouveau_memx *memx)
123 {
124 struct nouveau_pwr *ppwr = memx->ppwr;
125 u32 heads, x, y, px = 0;
126 int i, head_sync;
127
128 if (nv_device(ppwr)->chipset < 0xd0) {
129 heads = nv_rd32(ppwr, 0x610050);
130 for (i = 0; i < 2; i++) {
131 /* Heuristic: sync to head with biggest resolution */
132 if (heads & (2 << (i << 3))) {
133 x = nv_rd32(ppwr, 0x610b40 + (0x540 * i));
134 y = (x & 0xffff0000) >> 16;
135 x &= 0x0000ffff;
136 if ((x * y) > px) {
137 px = (x * y);
138 head_sync = i;
139 }
140 }
141 }
142 }
143
144 if (px == 0) {
145 nv_debug(memx->ppwr, "WAIT VBLANK !NO ACTIVE HEAD\n");
146 return;
147 }
148
149 nv_debug(memx->ppwr, "WAIT VBLANK HEAD%d\n", head_sync);
150 memx_cmd(memx, MEMX_VBLANK, 1, (u32[]){ head_sync });
151 memx_out(memx); /* fuc can't handle multiple */
152 }
153
154 void
nouveau_memx_block(struct nouveau_memx * memx)155 nouveau_memx_block(struct nouveau_memx *memx)
156 {
157 nv_debug(memx->ppwr, " HOST BLOCKED\n");
158 memx_cmd(memx, MEMX_ENTER, 0, NULL);
159 }
160
161 void
nouveau_memx_unblock(struct nouveau_memx * memx)162 nouveau_memx_unblock(struct nouveau_memx *memx)
163 {
164 nv_debug(memx->ppwr, " HOST UNBLOCKED\n");
165 memx_cmd(memx, MEMX_LEAVE, 0, NULL);
166 }
167
168 #endif
169