]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/subdev/ltcg/nvc0.c
cce65cc565145cb74fdfd513967cb5723c1afc8d
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / subdev / ltcg / nvc0.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/ltcg.h>
26 #include <subdev/fb.h>
27 #include <subdev/timer.h>
28
29 struct nvc0_ltcg_priv {
30         struct nouveau_ltcg base;
31         u32 part_nr;
32         u32 subp_nr;
33         u32 num_tags;
34         u32 tag_base;
35         struct nouveau_mm tags;
36         struct nouveau_mm_node *tag_ram;
37 };
38
39 static void
40 nvc0_ltcg_subp_isr(struct nvc0_ltcg_priv *priv, int unit, int subp)
41 {
42         u32 subp_base = 0x141000 + (unit * 0x2000) + (subp * 0x400);
43         u32 stat = nv_rd32(priv, subp_base + 0x020);
44
45         if (stat) {
46                 nv_info(priv, "LTC%d_LTS%d: 0x%08x\n", unit, subp, stat);
47                 nv_wr32(priv, subp_base + 0x020, stat);
48         }
49 }
50
51 static void
52 nvc0_ltcg_intr(struct nouveau_subdev *subdev)
53 {
54         struct nvc0_ltcg_priv *priv = (void *)subdev;
55         u32 units;
56
57         units = nv_rd32(priv, 0x00017c);
58         while (units) {
59                 u32 subp, unit = ffs(units) - 1;
60                 for (subp = 0; subp < priv->subp_nr; subp++)
61                         nvc0_ltcg_subp_isr(priv, unit, subp);
62                 units &= ~(1 << unit);
63         }
64
65         /* we do something horribly wrong and upset PMFB a lot, so mask off
66          * interrupts from it after the first one until it's fixed
67          */
68         nv_mask(priv, 0x000640, 0x02000000, 0x00000000);
69 }
70
71 static int
72 nvc0_ltcg_tags_alloc(struct nouveau_ltcg *ltcg, u32 n,
73                      struct nouveau_mm_node **pnode)
74 {
75         struct nvc0_ltcg_priv *priv = (struct nvc0_ltcg_priv *)ltcg;
76         int ret;
77
78         ret = nouveau_mm_head(&priv->tags, 1, n, n, 1, pnode);
79         if (ret)
80                 *pnode = NULL;
81
82         return ret;
83 }
84
85 static void
86 nvc0_ltcg_tags_free(struct nouveau_ltcg *ltcg, struct nouveau_mm_node **pnode)
87 {
88         struct nvc0_ltcg_priv *priv = (struct nvc0_ltcg_priv *)ltcg;
89
90         nouveau_mm_free(&priv->tags, pnode);
91 }
92
93 static void
94 nvc0_ltcg_tags_clear(struct nouveau_ltcg *ltcg, u32 first, u32 count)
95 {
96         struct nvc0_ltcg_priv *priv = (struct nvc0_ltcg_priv *)ltcg;
97         u32 last = first + count - 1;
98         int p, i;
99
100         BUG_ON((first > last) || (last >= priv->num_tags));
101
102         nv_wr32(priv, 0x17e8cc, first);
103         nv_wr32(priv, 0x17e8d0, last);
104         nv_wr32(priv, 0x17e8c8, 0x4); /* trigger clear */
105
106         /* wait until it's finished with clearing */
107         for (p = 0; p < priv->part_nr; ++p) {
108                 for (i = 0; i < priv->subp_nr; ++i)
109                         nv_wait(priv, 0x1410c8 + p * 0x2000 + i * 0x400, ~0, 0);
110         }
111 }
112
113 /* TODO: Figure out tag memory details and drop the over-cautious allocation.
114  */
115 static int
116 nvc0_ltcg_init_tag_ram(struct nouveau_fb *pfb, struct nvc0_ltcg_priv *priv)
117 {
118         u32 tag_size, tag_margin, tag_align;
119         int ret;
120
121         /* tags for 1/4 of VRAM should be enough (8192/4 per GiB of VRAM) */
122         priv->num_tags = (pfb->ram->size >> 17) / 4;
123         if (priv->num_tags > (1 << 17))
124                 priv->num_tags = 1 << 17; /* we have 17 bits in PTE */
125         priv->num_tags = (priv->num_tags + 63) & ~63; /* round up to 64 */
126
127         tag_align = priv->part_nr * 0x800;
128         tag_margin = (tag_align < 0x6000) ? 0x6000 : tag_align;
129
130         /* 4 part 4 sub: 0x2000 bytes for 56 tags */
131         /* 3 part 4 sub: 0x6000 bytes for 168 tags */
132         /*
133          * About 147 bytes per tag. Let's be safe and allocate x2, which makes
134          * 0x4980 bytes for 64 tags, and round up to 0x6000 bytes for 64 tags.
135          *
136          * For 4 GiB of memory we'll have 8192 tags which makes 3 MiB, < 0.1 %.
137          */
138         tag_size  = (priv->num_tags / 64) * 0x6000 + tag_margin;
139         tag_size += tag_align;
140         tag_size  = (tag_size + 0xfff) >> 12; /* round up */
141
142         ret = nouveau_mm_tail(&pfb->vram, 1, tag_size, tag_size, 1,
143                               &priv->tag_ram);
144         if (ret) {
145                 priv->num_tags = 0;
146         } else {
147                 u64 tag_base = (priv->tag_ram->offset << 12) + tag_margin;
148
149                 tag_base += tag_align - 1;
150                 ret = do_div(tag_base, tag_align);
151
152                 priv->tag_base = tag_base;
153         }
154         ret = nouveau_mm_init(&priv->tags, 0, priv->num_tags, 1);
155
156         return ret;
157 }
158
159 static int
160 nvc0_ltcg_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
161                struct nouveau_oclass *oclass, void *data, u32 size,
162                struct nouveau_object **pobject)
163 {
164         struct nvc0_ltcg_priv *priv;
165         struct nouveau_fb *pfb = nouveau_fb(parent);
166         u32 parts, mask;
167         int ret, i;
168
169         ret = nouveau_ltcg_create(parent, engine, oclass, &priv);
170         *pobject = nv_object(priv);
171         if (ret)
172                 return ret;
173
174         parts = nv_rd32(priv, 0x022438);
175         mask = nv_rd32(priv, 0x022554);
176         for (i = 0; i < parts; i++) {
177                 if (!(mask & (1 << i)))
178                         priv->part_nr++;
179         }
180         priv->subp_nr = nv_rd32(priv, 0x17e8dc) >> 28;
181
182         ret = nvc0_ltcg_init_tag_ram(pfb, priv);
183         if (ret)
184                 return ret;
185
186         priv->base.tags_alloc = nvc0_ltcg_tags_alloc;
187         priv->base.tags_free  = nvc0_ltcg_tags_free;
188         priv->base.tags_clear = nvc0_ltcg_tags_clear;
189
190         nv_subdev(priv)->intr = nvc0_ltcg_intr;
191         return 0;
192 }
193
194 static void
195 nvc0_ltcg_dtor(struct nouveau_object *object)
196 {
197         struct nouveau_ltcg *ltcg = (struct nouveau_ltcg *)object;
198         struct nvc0_ltcg_priv *priv = (struct nvc0_ltcg_priv *)ltcg;
199         struct nouveau_fb *pfb = nouveau_fb(ltcg->base.base.parent);
200
201         nouveau_mm_fini(&priv->tags);
202         nouveau_mm_free(&pfb->vram, &priv->tag_ram);
203
204         nouveau_ltcg_destroy(ltcg);
205 }
206
207 static int
208 nvc0_ltcg_init(struct nouveau_object *object)
209 {
210         struct nouveau_ltcg *ltcg = (struct nouveau_ltcg *)object;
211         struct nvc0_ltcg_priv *priv = (struct nvc0_ltcg_priv *)ltcg;
212         int ret;
213
214         ret = nouveau_ltcg_init(ltcg);
215         if (ret)
216                 return ret;
217
218         nv_mask(priv, 0x17e820, 0x00100000, 0x00000000); /* INTR_EN &= ~0x10 */
219         nv_wr32(priv, 0x17e8d8, priv->part_nr);
220         if (nv_device(ltcg)->card_type >= NV_E0)
221                 nv_wr32(priv, 0x17e000, priv->part_nr);
222         nv_wr32(priv, 0x17e8d4, priv->tag_base);
223         return 0;
224 }
225
226 struct nouveau_oclass
227 nvc0_ltcg_oclass = {
228         .handle = NV_SUBDEV(LTCG, 0xc0),
229         .ofuncs = &(struct nouveau_ofuncs) {
230                 .ctor = nvc0_ltcg_ctor,
231                 .dtor = nvc0_ltcg_dtor,
232                 .init = nvc0_ltcg_init,
233                 .fini = _nouveau_ltcg_fini,
234         },
235 };