Home | History | Annotate | Line # | Download | only in core
      1 /*	$NetBSD: nouveau_nvkm_core_memory.c,v 1.3 2021/12/18 23:45:34 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2015 Red Hat Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors: Ben Skeggs <bskeggs (at) redhat.com>
     25  */
     26 #include <sys/cdefs.h>
     27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_core_memory.c,v 1.3 2021/12/18 23:45:34 riastradh Exp $");
     28 
     29 #include <core/memory.h>
     30 #include <core/mm.h>
     31 #include <subdev/fb.h>
     32 #include <subdev/instmem.h>
     33 
     34 void
     35 nvkm_memory_tags_put(struct nvkm_memory *memory, struct nvkm_device *device,
     36 		     struct nvkm_tags **ptags)
     37 {
     38 	struct nvkm_fb *fb = device->fb;
     39 	struct nvkm_tags *tags = *ptags;
     40 	if (tags) {
     41 		mutex_lock(&fb->subdev.mutex);
     42 		if (refcount_dec_and_test(&tags->refcount)) {
     43 			nvkm_mm_free(&fb->tags, &tags->mn);
     44 			kfree(memory->tags);
     45 			memory->tags = NULL;
     46 		}
     47 		mutex_unlock(&fb->subdev.mutex);
     48 		*ptags = NULL;
     49 	}
     50 }
     51 
     52 int
     53 nvkm_memory_tags_get(struct nvkm_memory *memory, struct nvkm_device *device,
     54 		     u32 nr, void (*clr)(struct nvkm_device *, u32, u32),
     55 		     struct nvkm_tags **ptags)
     56 {
     57 	struct nvkm_fb *fb = device->fb;
     58 	struct nvkm_tags *tags;
     59 
     60 	mutex_lock(&fb->subdev.mutex);
     61 	if ((tags = memory->tags)) {
     62 		/* If comptags exist for the memory, but a different amount
     63 		 * than requested, the buffer is being mapped with settings
     64 		 * that are incompatible with existing mappings.
     65 		 */
     66 		if (tags->mn && tags->mn->length != nr) {
     67 			mutex_unlock(&fb->subdev.mutex);
     68 			return -EINVAL;
     69 		}
     70 
     71 		refcount_inc(&tags->refcount);
     72 		mutex_unlock(&fb->subdev.mutex);
     73 		*ptags = tags;
     74 		return 0;
     75 	}
     76 
     77 	if (!(tags = kmalloc(sizeof(*tags), GFP_KERNEL))) {
     78 		mutex_unlock(&fb->subdev.mutex);
     79 		return -ENOMEM;
     80 	}
     81 
     82 	if (!nvkm_mm_head(&fb->tags, 0, 1, nr, nr, 1, &tags->mn)) {
     83 		if (clr)
     84 			clr(device, tags->mn->offset, tags->mn->length);
     85 	} else {
     86 		/* Failure to allocate HW comptags is not an error, the
     87 		 * caller should fall back to an uncompressed map.
     88 		 *
     89 		 * As memory can be mapped in multiple places, we still
     90 		 * need to track the allocation failure and ensure that
     91 		 * any additional mappings remain uncompressed.
     92 		 *
     93 		 * This is handled by returning an empty nvkm_tags.
     94 		 */
     95 		tags->mn = NULL;
     96 	}
     97 
     98 	refcount_set(&tags->refcount, 1);
     99 	*ptags = memory->tags = tags;
    100 	mutex_unlock(&fb->subdev.mutex);
    101 	return 0;
    102 }
    103 
    104 void
    105 nvkm_memory_ctor(const struct nvkm_memory_func *func,
    106 		 struct nvkm_memory *memory)
    107 {
    108 	memory->func = func;
    109 	kref_init(&memory->kref);
    110 }
    111 
    112 static void
    113 nvkm_memory_del(struct kref *kref)
    114 {
    115 	struct nvkm_memory *memory = container_of(kref, typeof(*memory), kref);
    116 	if (!WARN_ON(!memory->func)) {
    117 		if (memory->func->dtor)
    118 			memory = memory->func->dtor(memory);
    119 		kfree(memory);
    120 	}
    121 }
    122 
    123 void
    124 nvkm_memory_unref(struct nvkm_memory **pmemory)
    125 {
    126 	struct nvkm_memory *memory = *pmemory;
    127 	if (memory) {
    128 		kref_put(&memory->kref, nvkm_memory_del);
    129 		*pmemory = NULL;
    130 	}
    131 }
    132 
    133 struct nvkm_memory *
    134 nvkm_memory_ref(struct nvkm_memory *memory)
    135 {
    136 	if (memory)
    137 		kref_get(&memory->kref);
    138 	return memory;
    139 }
    140 
    141 int
    142 nvkm_memory_new(struct nvkm_device *device, enum nvkm_memory_target target,
    143 		u64 size, u32 align, bool zero,
    144 		struct nvkm_memory **pmemory)
    145 {
    146 	struct nvkm_instmem *imem = device->imem;
    147 	struct nvkm_memory *memory;
    148 	int ret = -ENOSYS;
    149 
    150 	if (unlikely(target != NVKM_MEM_TARGET_INST || !imem))
    151 		return -ENOSYS;
    152 
    153 	ret = nvkm_instobj_new(imem, size, align, zero, &memory);
    154 	if (ret)
    155 		return ret;
    156 
    157 	*pmemory = memory;
    158 	return 0;
    159 }
    160