0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
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
0058
0059
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
0082
0083
0084
0085
0086
0087
0088
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 }