Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2015 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Ben Skeggs <bskeggs@redhat.com>
0023  */
0024 #include <core/memory.h>
0025 #include <core/mm.h>
0026 #include <subdev/fb.h>
0027 #include <subdev/instmem.h>
0028 
0029 void
0030 nvkm_memory_tags_put(struct nvkm_memory *memory, struct nvkm_device *device,
0031              struct nvkm_tags **ptags)
0032 {
0033     struct nvkm_fb *fb = device->fb;
0034     struct nvkm_tags *tags = *ptags;
0035     if (tags) {
0036         mutex_lock(&fb->tags.mutex);
0037         if (refcount_dec_and_test(&tags->refcount)) {
0038             nvkm_mm_free(&fb->tags.mm, &tags->mn);
0039             kfree(memory->tags);
0040             memory->tags = NULL;
0041         }
0042         mutex_unlock(&fb->tags.mutex);
0043         *ptags = NULL;
0044     }
0045 }
0046 
0047 int
0048 nvkm_memory_tags_get(struct nvkm_memory *memory, struct nvkm_device *device,
0049              u32 nr, void (*clr)(struct nvkm_device *, u32, u32),
0050              struct nvkm_tags **ptags)
0051 {
0052     struct nvkm_fb *fb = device->fb;
0053     struct nvkm_tags *tags;
0054 
0055     mutex_lock(&fb->tags.mutex);
0056     if ((tags = memory->tags)) {
0057         /* If comptags exist for the memory, but a different amount
0058          * than requested, the buffer is being mapped with settings
0059          * that are incompatible with existing mappings.
0060          */
0061         if (tags->mn && tags->mn->length != nr) {
0062             mutex_unlock(&fb->tags.mutex);
0063             return -EINVAL;
0064         }
0065 
0066         refcount_inc(&tags->refcount);
0067         mutex_unlock(&fb->tags.mutex);
0068         *ptags = tags;
0069         return 0;
0070     }
0071 
0072     if (!(tags = kmalloc(sizeof(*tags), GFP_KERNEL))) {
0073         mutex_unlock(&fb->tags.mutex);
0074         return -ENOMEM;
0075     }
0076 
0077     if (!nvkm_mm_head(&fb->tags.mm, 0, 1, nr, nr, 1, &tags->mn)) {
0078         if (clr)
0079             clr(device, tags->mn->offset, tags->mn->length);
0080     } else {
0081         /* Failure to allocate HW comptags is not an error, the
0082          * caller should fall back to an uncompressed map.
0083          *
0084          * As memory can be mapped in multiple places, we still
0085          * need to track the allocation failure and ensure that
0086          * any additional mappings remain uncompressed.
0087          *
0088          * This is handled by returning an empty nvkm_tags.
0089          */
0090         tags->mn = NULL;
0091     }
0092 
0093     refcount_set(&tags->refcount, 1);
0094     *ptags = memory->tags = tags;
0095     mutex_unlock(&fb->tags.mutex);
0096     return 0;
0097 }
0098 
0099 void
0100 nvkm_memory_ctor(const struct nvkm_memory_func *func,
0101          struct nvkm_memory *memory)
0102 {
0103     memory->func = func;
0104     kref_init(&memory->kref);
0105 }
0106 
0107 static void
0108 nvkm_memory_del(struct kref *kref)
0109 {
0110     struct nvkm_memory *memory = container_of(kref, typeof(*memory), kref);
0111     if (!WARN_ON(!memory->func)) {
0112         if (memory->func->dtor)
0113             memory = memory->func->dtor(memory);
0114         kfree(memory);
0115     }
0116 }
0117 
0118 void
0119 nvkm_memory_unref(struct nvkm_memory **pmemory)
0120 {
0121     struct nvkm_memory *memory = *pmemory;
0122     if (memory) {
0123         kref_put(&memory->kref, nvkm_memory_del);
0124         *pmemory = NULL;
0125     }
0126 }
0127 
0128 struct nvkm_memory *
0129 nvkm_memory_ref(struct nvkm_memory *memory)
0130 {
0131     if (memory)
0132         kref_get(&memory->kref);
0133     return memory;
0134 }
0135 
0136 int
0137 nvkm_memory_new(struct nvkm_device *device, enum nvkm_memory_target target,
0138         u64 size, u32 align, bool zero,
0139         struct nvkm_memory **pmemory)
0140 {
0141     struct nvkm_instmem *imem = device->imem;
0142     struct nvkm_memory *memory;
0143     int ret;
0144 
0145     if (unlikely(target != NVKM_MEM_TARGET_INST || !imem))
0146         return -ENOSYS;
0147 
0148     ret = nvkm_instobj_new(imem, size, align, zero, &memory);
0149     if (ret)
0150         return ret;
0151 
0152     *pmemory = memory;
0153     return 0;
0154 }