drm/nouveau/instmem/gk20a: move memory allocation to instmem
[linux-2.6-microblaze.git] / drivers / gpu / drm / nouveau / nvkm / subdev / instmem / gk20a.c
1 /*
2  * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #include <subdev/fb.h>
24 #include <core/mm.h>
25 #include <core/device.h>
26
27 #include "priv.h"
28
29 struct gk20a_instobj_priv {
30         struct nvkm_instobj base;
31         /* Must be second member here - see nouveau_gpuobj_map_vm() */
32         struct nvkm_mem *mem;
33         /* Pointed by mem */
34         struct nvkm_mem _mem;
35         void *cpuaddr;
36         dma_addr_t handle;
37         struct nvkm_mm_node r;
38 };
39
40 struct gk20a_instmem_priv {
41         struct nvkm_instmem base;
42         spinlock_t lock;
43         u64 addr;
44 };
45
46 static u32
47 gk20a_instobj_rd32(struct nvkm_object *object, u64 offset)
48 {
49         struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(object);
50         struct gk20a_instobj_priv *node = (void *)object;
51         unsigned long flags;
52         u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
53         u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
54         u32 data;
55
56         spin_lock_irqsave(&priv->lock, flags);
57         if (unlikely(priv->addr != base)) {
58                 nv_wr32(priv, 0x001700, base >> 16);
59                 priv->addr = base;
60         }
61         data = nv_rd32(priv, 0x700000 + addr);
62         spin_unlock_irqrestore(&priv->lock, flags);
63         return data;
64 }
65
66 static void
67 gk20a_instobj_wr32(struct nvkm_object *object, u64 offset, u32 data)
68 {
69         struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(object);
70         struct gk20a_instobj_priv *node = (void *)object;
71         unsigned long flags;
72         u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
73         u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
74
75         spin_lock_irqsave(&priv->lock, flags);
76         if (unlikely(priv->addr != base)) {
77                 nv_wr32(priv, 0x001700, base >> 16);
78                 priv->addr = base;
79         }
80         nv_wr32(priv, 0x700000 + addr, data);
81         spin_unlock_irqrestore(&priv->lock, flags);
82 }
83
84 static void
85 gk20a_instobj_dtor(struct nvkm_object *object)
86 {
87         struct gk20a_instobj_priv *node = (void *)object;
88         struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(node);
89         struct device *dev = nv_device_base(nv_device(priv));
90
91         if (unlikely(!node->handle))
92                 return;
93
94         dma_free_coherent(dev, node->mem->size << PAGE_SHIFT, node->cpuaddr,
95                           node->handle);
96
97         nvkm_instobj_destroy(&node->base);
98 }
99
100 static int
101 gk20a_instobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
102                    struct nvkm_oclass *oclass, void *data, u32 _size,
103                    struct nvkm_object **pobject)
104 {
105         struct nvkm_instobj_args *args = data;
106         struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(parent);
107         struct device *dev = nv_device_base(nv_device(priv));
108         struct gk20a_instobj_priv *node;
109         u32 size, align;
110         u32 npages;
111         int ret;
112
113         nv_debug(parent, "%s: size: %x align: %x\n", __func__,
114                  args->size, args->align);
115
116         size  = max((args->size  + 4095) & ~4095, (u32)4096);
117         align = max((args->align + 4095) & ~4095, (u32)4096);
118
119         npages = size >> PAGE_SHIFT;
120
121         ret = nvkm_instobj_create_(parent, engine, oclass, sizeof(*node),
122                                       (void **)&node);
123         *pobject = nv_object(node);
124         if (ret)
125                 return ret;
126
127         node->mem = &node->_mem;
128
129         node->cpuaddr = dma_alloc_coherent(dev, npages << PAGE_SHIFT,
130                                            &node->handle, GFP_KERNEL);
131         if (!node->cpuaddr) {
132                 nv_error(priv, "cannot allocate DMA memory\n");
133                 return -ENOMEM;
134         }
135
136         /* alignment check */
137         if (unlikely(node->handle & (align - 1)))
138                 nv_warn(priv, "memory not aligned as requested: %pad (0x%x)\n",
139                         &node->handle, align);
140
141         node->mem->offset = node->handle;
142         node->mem->size = size >> 12;
143         node->mem->memtype = 0;
144         node->mem->page_shift = 12;
145         INIT_LIST_HEAD(&node->mem->regions);
146
147         node->r.type = 12;
148         node->r.offset = node->handle >> 12;
149         node->r.length = npages;
150         list_add_tail(&node->r.rl_entry, &node->mem->regions);
151
152         node->base.addr = node->mem->offset;
153         node->base.size = size;
154
155         nv_debug(parent, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
156                  size, align, node->mem->offset);
157
158         return 0;
159 }
160
161 static struct nvkm_instobj_impl
162 gk20a_instobj_oclass = {
163         .base.ofuncs = &(struct nvkm_ofuncs) {
164                 .ctor = gk20a_instobj_ctor,
165                 .dtor = gk20a_instobj_dtor,
166                 .init = _nvkm_instobj_init,
167                 .fini = _nvkm_instobj_fini,
168                 .rd32 = gk20a_instobj_rd32,
169                 .wr32 = gk20a_instobj_wr32,
170         },
171 };
172
173
174
175 static int
176 gk20a_instmem_fini(struct nvkm_object *object, bool suspend)
177 {
178         struct gk20a_instmem_priv *priv = (void *)object;
179         priv->addr = ~0ULL;
180         return nvkm_instmem_fini(&priv->base, suspend);
181 }
182
183 static int
184 gk20a_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
185                    struct nvkm_oclass *oclass, void *data, u32 size,
186                    struct nvkm_object **pobject)
187 {
188         struct gk20a_instmem_priv *priv;
189         int ret;
190
191         ret = nvkm_instmem_create(parent, engine, oclass, &priv);
192         *pobject = nv_object(priv);
193         if (ret)
194                 return ret;
195
196         spin_lock_init(&priv->lock);
197
198         return 0;
199 }
200
201 struct nvkm_oclass *
202 gk20a_instmem_oclass = &(struct nvkm_instmem_impl) {
203         .base.handle = NV_SUBDEV(INSTMEM, 0xea),
204         .base.ofuncs = &(struct nvkm_ofuncs) {
205                 .ctor = gk20a_instmem_ctor,
206                 .dtor = _nvkm_instmem_dtor,
207                 .init = _nvkm_instmem_init,
208                 .fini = gk20a_instmem_fini,
209         },
210         .instobj = &gk20a_instobj_oclass.base,
211 }.base;