Linux-libre 3.14.42-gnu
[librecmc/linux-libre.git] / drivers / gpu / drm / nouveau / core / subdev / pwr / memx.c
1 #ifndef __NVKM_PWR_MEMX_H__
2 #define __NVKM_PWR_MEMX_H__
3
4 #include <subdev/pwr.h>
5 #include <subdev/pwr/fuc/os.h>
6
7 struct nouveau_memx {
8         struct nouveau_pwr *ppwr;
9         u32 base;
10         u32 size;
11         struct {
12                 u32 mthd;
13                 u32 size;
14                 u32 data[64];
15         } c;
16 };
17
18 static void
19 memx_out(struct nouveau_memx *memx)
20 {
21         struct nouveau_pwr *ppwr = memx->ppwr;
22         int i;
23
24         if (memx->c.size) {
25                 nv_wr32(ppwr, 0x10a1c4, (memx->c.size << 16) | memx->c.mthd);
26                 for (i = 0; i < memx->c.size; i++)
27                         nv_wr32(ppwr, 0x10a1c4, memx->c.data[i]);
28                 memx->c.size = 0;
29         }
30 }
31
32 static void
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.size && 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
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         nv_wr32(ppwr, 0x10a1c4, 0x00010000 | MEMX_ENTER);
67         nv_wr32(ppwr, 0x10a1c4, 0x00000000);
68         return 0;
69 }
70
71 int
72 nouveau_memx_fini(struct nouveau_memx **pmemx, bool exec)
73 {
74         struct nouveau_memx *memx = *pmemx;
75         struct nouveau_pwr *ppwr = memx->ppwr;
76         u32 finish, reply[2];
77
78         /* flush the cache... */
79         memx_out(memx);
80
81         /* release data segment access */
82         nv_wr32(ppwr, 0x10a1c4, 0x00000000 | MEMX_LEAVE);
83         finish = nv_rd32(ppwr, 0x10a1c0) & 0x00ffffff;
84         nv_wr32(ppwr, 0x10a580, 0x00000000);
85
86         /* call MEMX process to execute the script, and wait for reply */
87         if (exec) {
88                 ppwr->message(ppwr, reply, PROC_MEMX, MEMX_MSG_EXEC,
89                                  memx->base, finish);
90         }
91
92         kfree(memx);
93         return 0;
94 }
95
96 void
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
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
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 #endif