drm/nouveau/ltc: add zbc drivers
[pandora-kernel.git] / drivers / gpu / drm / nouveau / core / subdev / ltc / gf100.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <subdev/fb.h>
26 #include <subdev/timer.h>
27
28 #include "priv.h"
29
30 void
31 gf100_ltc_cbc_clear(struct nvkm_ltc_priv *priv, u32 start, u32 limit)
32 {
33         nv_wr32(priv, 0x17e8cc, start);
34         nv_wr32(priv, 0x17e8d0, limit);
35         nv_wr32(priv, 0x17e8c8, 0x00000004);
36 }
37
38 void
39 gf100_ltc_cbc_wait(struct nvkm_ltc_priv *priv)
40 {
41         int c, s;
42         for (c = 0; c < priv->ltc_nr; c++) {
43                 for (s = 0; s < priv->lts_nr; s++)
44                         nv_wait(priv, 0x1410c8 + c * 0x2000 + s * 0x400, ~0, 0);
45         }
46 }
47
48 void
49 gf100_ltc_zbc_clear_color(struct nvkm_ltc_priv *priv, int i, const u32 color[4])
50 {
51         nv_mask(priv, 0x17ea44, 0x0000000f, i);
52         nv_wr32(priv, 0x17ea48, color[0]);
53         nv_wr32(priv, 0x17ea4c, color[1]);
54         nv_wr32(priv, 0x17ea50, color[2]);
55         nv_wr32(priv, 0x17ea54, color[3]);
56 }
57
58 void
59 gf100_ltc_zbc_clear_depth(struct nvkm_ltc_priv *priv, int i, const u32 depth)
60 {
61         nv_mask(priv, 0x17ea44, 0x0000000f, i);
62         nv_wr32(priv, 0x17ea58, depth);
63 }
64
65 static void
66 gf100_ltc_lts_isr(struct nvkm_ltc_priv *priv, int ltc, int lts)
67 {
68         u32 base = 0x141000 + (ltc * 0x2000) + (lts * 0x400);
69         u32 stat = nv_rd32(priv, base + 0x020);
70
71         if (stat) {
72                 nv_info(priv, "LTC%d_LTS%d: 0x%08x\n", ltc, lts, stat);
73                 nv_wr32(priv, base + 0x020, stat);
74         }
75 }
76
77 void
78 gf100_ltc_intr(struct nouveau_subdev *subdev)
79 {
80         struct nvkm_ltc_priv *priv = (void *)subdev;
81         u32 mask;
82
83         mask = nv_rd32(priv, 0x00017c);
84         while (mask) {
85                 u32 lts, ltc = __ffs(mask);
86                 for (lts = 0; lts < priv->lts_nr; lts++)
87                         gf100_ltc_lts_isr(priv, ltc, lts);
88                 mask &= ~(1 << ltc);
89         }
90
91         /* we do something horribly wrong and upset PMFB a lot, so mask off
92          * interrupts from it after the first one until it's fixed
93          */
94         nv_mask(priv, 0x000640, 0x02000000, 0x00000000);
95 }
96
97 static int
98 gf100_ltc_init(struct nouveau_object *object)
99 {
100         struct nvkm_ltc_priv *priv = (void *)object;
101         int ret;
102
103         ret = nvkm_ltc_init(priv);
104         if (ret)
105                 return ret;
106
107         nv_mask(priv, 0x17e820, 0x00100000, 0x00000000); /* INTR_EN &= ~0x10 */
108         nv_wr32(priv, 0x17e8d8, priv->ltc_nr);
109         nv_wr32(priv, 0x17e8d4, priv->tag_base);
110         return 0;
111 }
112
113 void
114 gf100_ltc_dtor(struct nouveau_object *object)
115 {
116         struct nouveau_fb *pfb = nouveau_fb(object);
117         struct nvkm_ltc_priv *priv = (void *)object;
118
119         nouveau_mm_fini(&priv->tags);
120         nouveau_mm_free(&pfb->vram, &priv->tag_ram);
121
122         nvkm_ltc_destroy(priv);
123 }
124
125 /* TODO: Figure out tag memory details and drop the over-cautious allocation.
126  */
127 int
128 gf100_ltc_init_tag_ram(struct nouveau_fb *pfb, struct nvkm_ltc_priv *priv)
129 {
130         u32 tag_size, tag_margin, tag_align;
131         int ret;
132
133         /* tags for 1/4 of VRAM should be enough (8192/4 per GiB of VRAM) */
134         priv->num_tags = (pfb->ram->size >> 17) / 4;
135         if (priv->num_tags > (1 << 17))
136                 priv->num_tags = 1 << 17; /* we have 17 bits in PTE */
137         priv->num_tags = (priv->num_tags + 63) & ~63; /* round up to 64 */
138
139         tag_align = priv->ltc_nr * 0x800;
140         tag_margin = (tag_align < 0x6000) ? 0x6000 : tag_align;
141
142         /* 4 part 4 sub: 0x2000 bytes for 56 tags */
143         /* 3 part 4 sub: 0x6000 bytes for 168 tags */
144         /*
145          * About 147 bytes per tag. Let's be safe and allocate x2, which makes
146          * 0x4980 bytes for 64 tags, and round up to 0x6000 bytes for 64 tags.
147          *
148          * For 4 GiB of memory we'll have 8192 tags which makes 3 MiB, < 0.1 %.
149          */
150         tag_size  = (priv->num_tags / 64) * 0x6000 + tag_margin;
151         tag_size += tag_align;
152         tag_size  = (tag_size + 0xfff) >> 12; /* round up */
153
154         ret = nouveau_mm_tail(&pfb->vram, 1, tag_size, tag_size, 1,
155                               &priv->tag_ram);
156         if (ret) {
157                 priv->num_tags = 0;
158         } else {
159                 u64 tag_base = (priv->tag_ram->offset << 12) + tag_margin;
160
161                 tag_base += tag_align - 1;
162                 ret = do_div(tag_base, tag_align);
163
164                 priv->tag_base = tag_base;
165         }
166
167         ret = nouveau_mm_init(&priv->tags, 0, priv->num_tags, 1);
168         return ret;
169 }
170
171 int
172 gf100_ltc_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
173                struct nouveau_oclass *oclass, void *data, u32 size,
174                struct nouveau_object **pobject)
175 {
176         struct nouveau_fb *pfb = nouveau_fb(parent);
177         struct nvkm_ltc_priv *priv;
178         u32 parts, mask;
179         int ret, i;
180
181         ret = nvkm_ltc_create(parent, engine, oclass, &priv);
182         *pobject = nv_object(priv);
183         if (ret)
184                 return ret;
185
186         parts = nv_rd32(priv, 0x022438);
187         mask = nv_rd32(priv, 0x022554);
188         for (i = 0; i < parts; i++) {
189                 if (!(mask & (1 << i)))
190                         priv->ltc_nr++;
191         }
192         priv->lts_nr = nv_rd32(priv, 0x17e8dc) >> 28;
193
194         ret = gf100_ltc_init_tag_ram(pfb, priv);
195         if (ret)
196                 return ret;
197
198         nv_subdev(priv)->intr = gf100_ltc_intr;
199         return 0;
200 }
201
202 struct nouveau_oclass *
203 gf100_ltc_oclass = &(struct nvkm_ltc_impl) {
204         .base.handle = NV_SUBDEV(LTC, 0xc0),
205         .base.ofuncs = &(struct nouveau_ofuncs) {
206                 .ctor = gf100_ltc_ctor,
207                 .dtor = gf100_ltc_dtor,
208                 .init = gf100_ltc_init,
209                 .fini = _nvkm_ltc_fini,
210         },
211         .intr = gf100_ltc_intr,
212         .cbc_clear = gf100_ltc_cbc_clear,
213         .cbc_wait = gf100_ltc_cbc_wait,
214         .zbc = 16,
215         .zbc_clear_color = gf100_ltc_zbc_clear_color,
216         .zbc_clear_depth = gf100_ltc_zbc_clear_depth,
217 }.base;