e79d0e81de40700e403f0971477b99a48730bf5c
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / subdev / ltcg / gm107.c
1 /*
2  * Copyright 2014 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 "gf100.h"
29
30 static void
31 gm107_ltcg_lts_isr(struct gf100_ltcg_priv *priv, int ltc, int lts)
32 {
33         u32 base = 0x140000 + (ltc * 0x2000) + (lts * 0x400);
34         u32 stat = nv_rd32(priv, base + 0x00c);
35
36         if (stat) {
37                 nv_info(priv, "LTC%d_LTS%d: 0x%08x\n", ltc, lts, stat);
38                 nv_wr32(priv, base + 0x00c, stat);
39         }
40 }
41
42 static void
43 gm107_ltcg_intr(struct nouveau_subdev *subdev)
44 {
45         struct gf100_ltcg_priv *priv = (void *)subdev;
46         u32 mask;
47
48         mask = nv_rd32(priv, 0x00017c);
49         while (mask) {
50                 u32 lts, ltc = __ffs(mask);
51                 for (lts = 0; lts < priv->lts_nr; lts++)
52                         gm107_ltcg_lts_isr(priv, ltc, lts);
53                 mask &= ~(1 << ltc);
54         }
55
56         /* we do something horribly wrong and upset PMFB a lot, so mask off
57          * interrupts from it after the first one until it's fixed
58          */
59         nv_mask(priv, 0x000640, 0x02000000, 0x00000000);
60 }
61
62 static void
63 gm107_ltcg_tags_clear(struct nouveau_ltcg *ltcg, u32 first, u32 count)
64 {
65         struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
66         u32 last = first + count - 1;
67         int p, i;
68
69         BUG_ON((first > last) || (last >= priv->num_tags));
70
71         nv_wr32(priv, 0x17e270, first);
72         nv_wr32(priv, 0x17e274, last);
73         nv_wr32(priv, 0x17e26c, 0x4); /* trigger clear */
74
75         /* wait until it's finished with clearing */
76         for (p = 0; p < priv->ltc_nr; ++p) {
77                 for (i = 0; i < priv->lts_nr; ++i)
78                         nv_wait(priv, 0x14046c + p * 0x2000 + i * 0x200, ~0, 0);
79         }
80 }
81
82 static int
83 gm107_ltcg_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
84                struct nouveau_oclass *oclass, void *data, u32 size,
85                struct nouveau_object **pobject)
86 {
87         struct gf100_ltcg_priv *priv;
88         struct nouveau_fb *pfb = nouveau_fb(parent);
89         u32 parts, mask;
90         int ret, i;
91
92         ret = nouveau_ltcg_create(parent, engine, oclass, &priv);
93         *pobject = nv_object(priv);
94         if (ret)
95                 return ret;
96
97         parts = nv_rd32(priv, 0x022438);
98         mask = nv_rd32(priv, 0x021c14);
99         for (i = 0; i < parts; i++) {
100                 if (!(mask & (1 << i)))
101                         priv->ltc_nr++;
102         }
103         priv->lts_nr = nv_rd32(priv, 0x17e280) >> 28;
104
105         ret = gf100_ltcg_init_tag_ram(pfb, priv);
106         if (ret)
107                 return ret;
108
109         priv->base.tags_alloc = gf100_ltcg_tags_alloc;
110         priv->base.tags_free  = gf100_ltcg_tags_free;
111         priv->base.tags_clear = gm107_ltcg_tags_clear;
112
113         nv_subdev(priv)->intr = gm107_ltcg_intr;
114         return 0;
115 }
116
117 static int
118 gm107_ltcg_init(struct nouveau_object *object)
119 {
120         struct nouveau_ltcg *ltcg = (struct nouveau_ltcg *)object;
121         struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
122         int ret;
123
124         ret = nouveau_ltcg_init(ltcg);
125         if (ret)
126                 return ret;
127
128         nv_wr32(priv, 0x17e27c, priv->ltc_nr);
129         nv_wr32(priv, 0x17e278, priv->tag_base);
130         return 0;
131 }
132
133 struct nouveau_oclass *
134 gm107_ltcg_oclass = &(struct nouveau_oclass) {
135         .handle = NV_SUBDEV(LTCG, 0xff),
136         .ofuncs = &(struct nouveau_ofuncs) {
137                 .ctor = gm107_ltcg_ctor,
138                 .dtor = gf100_ltcg_dtor,
139                 .init = gm107_ltcg_init,
140                 .fini = _nouveau_ltcg_fini,
141         },
142 };