2 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 * GK20A does not have dedicated video memory, and to accurately represent this
25 * fact Nouveau will not create a RAM device for it. Therefore its instmem
26 * implementation must be done directly on top of system memory, while providing
27 * coherent read and write operations.
29 * Instmem can be allocated through two means:
30 * 1) If an IOMMU mapping has been probed, the IOMMU API is used to make memory
31 * pages contiguous to the GPU. This is the preferred way.
32 * 2) If no IOMMU mapping is probed, the DMA API is used to allocate physically
35 * In both cases CPU read and writes are performed using PRAMIN (i.e. using the
36 * GPU path) to ensure these operations are coherent for the GPU. This allows us
37 * to use more "relaxed" allocation parameters when using the DMA API, since we
38 * never need a kernel mapping.
41 #include <subdev/fb.h>
43 #include <core/device.h>
46 #include <linux/dma-attrs.h>
47 #include <linux/iommu.h>
48 #include <nouveau_platform.h>
53 struct gk20a_instobj_priv {
54 struct nvkm_instobj base;
55 /* Must be second member here - see nouveau_gpuobj_map_vm() */
62 * Used for objects allocated using the DMA API
64 struct gk20a_instobj_dma {
65 struct gk20a_instobj_priv base;
69 struct nvkm_mm_node r;
73 * Used for objects flattened using the IOMMU API
75 struct gk20a_instobj_iommu {
76 struct gk20a_instobj_priv base;
78 /* array of base.mem->size pages */
82 struct gk20a_instmem_priv {
83 struct nvkm_instmem base;
87 /* Only used if IOMMU if present */
88 struct mutex *mm_mutex;
90 struct iommu_domain *domain;
91 unsigned long iommu_pgshift;
93 /* Only used by DMA API */
94 struct dma_attrs attrs;
98 * Use PRAMIN to read/write data and avoid coherency issues.
99 * PRAMIN uses the GPU path and ensures data will always be coherent.
101 * A dynamic mapping based solution would be desirable in the future, but
102 * the issue remains of how to maintain coherency efficiently. On ARM it is
103 * not easy (if possible at all?) to create uncached temporary mappings.
107 gk20a_instobj_rd32(struct nvkm_object *object, u64 offset)
109 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(object);
110 struct gk20a_instobj_priv *node = (void *)object;
112 u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
113 u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
116 spin_lock_irqsave(&priv->lock, flags);
117 if (unlikely(priv->addr != base)) {
118 nv_wr32(priv, 0x001700, base >> 16);
121 data = nv_rd32(priv, 0x700000 + addr);
122 spin_unlock_irqrestore(&priv->lock, flags);
127 gk20a_instobj_wr32(struct nvkm_object *object, u64 offset, u32 data)
129 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(object);
130 struct gk20a_instobj_priv *node = (void *)object;
132 u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
133 u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
135 spin_lock_irqsave(&priv->lock, flags);
136 if (unlikely(priv->addr != base)) {
137 nv_wr32(priv, 0x001700, base >> 16);
140 nv_wr32(priv, 0x700000 + addr, data);
141 spin_unlock_irqrestore(&priv->lock, flags);
145 gk20a_instobj_dtor_dma(struct gk20a_instobj_priv *_node)
147 struct gk20a_instobj_dma *node = (void *)_node;
148 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(node);
149 struct device *dev = nv_device_base(nv_device(priv));
151 if (unlikely(!node->cpuaddr))
154 dma_free_attrs(dev, _node->mem->size << PAGE_SHIFT, node->cpuaddr,
155 node->handle, &priv->attrs);
159 gk20a_instobj_dtor_iommu(struct gk20a_instobj_priv *_node)
161 struct gk20a_instobj_iommu *node = (void *)_node;
162 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(node);
163 struct nvkm_mm_node *r;
166 if (unlikely(list_empty(&_node->mem->regions)))
169 r = list_first_entry(&_node->mem->regions, struct nvkm_mm_node,
172 /* clear bit 34 to unmap pages */
173 r->offset &= ~BIT(34 - priv->iommu_pgshift);
175 /* Unmap pages from GPU address space and free them */
176 for (i = 0; i < _node->mem->size; i++) {
177 iommu_unmap(priv->domain,
178 (r->offset + i) << priv->iommu_pgshift, PAGE_SIZE);
179 __free_page(node->pages[i]);
182 /* Release area from GPU address space */
183 mutex_lock(priv->mm_mutex);
184 nvkm_mm_free(priv->mm, &r);
185 mutex_unlock(priv->mm_mutex);
189 gk20a_instobj_dtor(struct nvkm_object *object)
191 struct gk20a_instobj_priv *node = (void *)object;
192 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(node);
195 gk20a_instobj_dtor_iommu(node);
197 gk20a_instobj_dtor_dma(node);
199 nvkm_instobj_destroy(&node->base);
203 gk20a_instobj_ctor_dma(struct nvkm_object *parent, struct nvkm_object *engine,
204 struct nvkm_oclass *oclass, u32 npages, u32 align,
205 struct gk20a_instobj_priv **_node)
207 struct gk20a_instobj_dma *node;
208 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(parent);
209 struct device *dev = nv_device_base(nv_device(parent));
212 ret = nvkm_instobj_create_(parent, engine, oclass, sizeof(*node),
214 *_node = &node->base;
218 node->cpuaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
219 &node->handle, GFP_KERNEL,
221 if (!node->cpuaddr) {
222 nv_error(priv, "cannot allocate DMA memory\n");
226 /* alignment check */
227 if (unlikely(node->handle & (align - 1)))
228 nv_warn(priv, "memory not aligned as requested: %pad (0x%x)\n",
229 &node->handle, align);
231 /* present memory for being mapped using small pages */
233 node->r.offset = node->handle >> 12;
234 node->r.length = (npages << PAGE_SHIFT) >> 12;
236 node->base._mem.offset = node->handle;
238 INIT_LIST_HEAD(&node->base._mem.regions);
239 list_add_tail(&node->r.rl_entry, &node->base._mem.regions);
245 gk20a_instobj_ctor_iommu(struct nvkm_object *parent, struct nvkm_object *engine,
246 struct nvkm_oclass *oclass, u32 npages, u32 align,
247 struct gk20a_instobj_priv **_node)
249 struct gk20a_instobj_iommu *node;
250 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(parent);
251 struct nvkm_mm_node *r;
255 ret = nvkm_instobj_create_(parent, engine, oclass,
256 sizeof(*node) + sizeof(node->pages[0]) * npages,
258 *_node = &node->base;
262 /* Allocate backing memory */
263 for (i = 0; i < npages; i++) {
264 struct page *p = alloc_page(GFP_KERNEL);
273 mutex_lock(priv->mm_mutex);
274 /* Reserve area from GPU address space */
275 ret = nvkm_mm_head(priv->mm, 0, 1, npages, npages,
276 align >> priv->iommu_pgshift, &r);
277 mutex_unlock(priv->mm_mutex);
279 nv_error(priv, "virtual space is full!\n");
283 /* Map into GPU address space */
284 for (i = 0; i < npages; i++) {
285 struct page *p = node->pages[i];
286 u32 offset = (r->offset + i) << priv->iommu_pgshift;
288 ret = iommu_map(priv->domain, offset, page_to_phys(p),
289 PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
291 nv_error(priv, "IOMMU mapping failure: %d\n", ret);
295 iommu_unmap(priv->domain, offset, PAGE_SIZE);
301 /* Bit 34 tells that an address is to be resolved through the IOMMU */
302 r->offset |= BIT(34 - priv->iommu_pgshift);
304 node->base._mem.offset = ((u64)r->offset) << priv->iommu_pgshift;
306 INIT_LIST_HEAD(&node->base._mem.regions);
307 list_add_tail(&r->rl_entry, &node->base._mem.regions);
312 mutex_lock(priv->mm_mutex);
313 nvkm_mm_free(priv->mm, &r);
314 mutex_unlock(priv->mm_mutex);
317 for (i = 0; i < npages && node->pages[i] != NULL; i++)
318 __free_page(node->pages[i]);
324 gk20a_instobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
325 struct nvkm_oclass *oclass, void *data, u32 _size,
326 struct nvkm_object **pobject)
328 struct nvkm_instobj_args *args = data;
329 struct gk20a_instmem_priv *priv = (void *)nvkm_instmem(parent);
330 struct gk20a_instobj_priv *node;
334 nv_debug(parent, "%s (%s): size: %x align: %x\n", __func__,
335 priv->domain ? "IOMMU" : "DMA", args->size, args->align);
337 /* Round size and align to page bounds */
338 size = max(roundup(args->size, PAGE_SIZE), PAGE_SIZE);
339 align = max(roundup(args->align, PAGE_SIZE), PAGE_SIZE);
342 ret = gk20a_instobj_ctor_iommu(parent, engine, oclass,
343 size >> PAGE_SHIFT, align, &node);
345 ret = gk20a_instobj_ctor_dma(parent, engine, oclass,
346 size >> PAGE_SHIFT, align, &node);
347 *pobject = nv_object(node);
351 node->mem = &node->_mem;
353 /* present memory for being mapped using small pages */
354 node->mem->size = size >> 12;
355 node->mem->memtype = 0;
356 node->mem->page_shift = 12;
358 node->base.addr = node->mem->offset;
359 node->base.size = size;
361 nv_debug(parent, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
362 size, align, node->mem->offset);
367 static struct nvkm_instobj_impl
368 gk20a_instobj_oclass = {
369 .base.ofuncs = &(struct nvkm_ofuncs) {
370 .ctor = gk20a_instobj_ctor,
371 .dtor = gk20a_instobj_dtor,
372 .init = _nvkm_instobj_init,
373 .fini = _nvkm_instobj_fini,
374 .rd32 = gk20a_instobj_rd32,
375 .wr32 = gk20a_instobj_wr32,
382 gk20a_instmem_fini(struct nvkm_object *object, bool suspend)
384 struct gk20a_instmem_priv *priv = (void *)object;
386 return nvkm_instmem_fini(&priv->base, suspend);
390 gk20a_instmem_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
391 struct nvkm_oclass *oclass, void *data, u32 size,
392 struct nvkm_object **pobject)
394 struct gk20a_instmem_priv *priv;
395 struct nouveau_platform_device *plat;
398 ret = nvkm_instmem_create(parent, engine, oclass, &priv);
399 *pobject = nv_object(priv);
403 spin_lock_init(&priv->lock);
405 plat = nv_device_to_platform(nv_device(parent));
406 if (plat->gpu->iommu.domain) {
407 priv->domain = plat->gpu->iommu.domain;
408 priv->mm = plat->gpu->iommu.mm;
409 priv->iommu_pgshift = plat->gpu->iommu.pgshift;
410 priv->mm_mutex = &plat->gpu->iommu.mutex;
412 nv_info(priv, "using IOMMU\n");
414 init_dma_attrs(&priv->attrs);
416 * We will access instmem through PRAMIN and thus do not need a
417 * consistent CPU pointer or kernel mapping
419 dma_set_attr(DMA_ATTR_NON_CONSISTENT, &priv->attrs);
420 dma_set_attr(DMA_ATTR_WEAK_ORDERING, &priv->attrs);
421 dma_set_attr(DMA_ATTR_WRITE_COMBINE, &priv->attrs);
422 dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &priv->attrs);
424 nv_info(priv, "using DMA API\n");
431 gk20a_instmem_oclass = &(struct nvkm_instmem_impl) {
432 .base.handle = NV_SUBDEV(INSTMEM, 0xea),
433 .base.ofuncs = &(struct nvkm_ofuncs) {
434 .ctor = gk20a_instmem_ctor,
435 .dtor = _nvkm_instmem_dtor,
436 .init = _nvkm_instmem_init,
437 .fini = gk20a_instmem_fini,
439 .instobj = &gk20a_instobj_oclass.base,