]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/fb/ram.c
5305078f730e731ee7b3af28644bf064ecbff06d
2 * Copyright 2015 Red Hat Inc.
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
24 #define nvkm_vram(p) container_of((p), struct nvkm_vram, memory)
27 #include <core/memory.h>
28 #include <subdev/mmu.h>
31 struct nvkm_memory memory
;
34 struct nvkm_mm_node
*mn
;
38 nvkm_vram_map(struct nvkm_memory
*memory
, u64 offset
, struct nvkm_vmm
*vmm
,
39 struct nvkm_vma
*vma
, void *argv
, u32 argc
)
41 struct nvkm_vram
*vram
= nvkm_vram(memory
);
42 struct nvkm_vmm_map map
= {
43 .memory
= &vram
->memory
,
49 struct nvkm_mem mem
= {
51 .memory
= &vram
->memory
,
53 nvkm_vm_map_at(vma
, offset
, &mem
);
57 return nvkm_vmm_map(vmm
, vma
, argv
, argc
, &map
);
61 nvkm_vram_size(struct nvkm_memory
*memory
)
63 return (u64
)nvkm_mm_size(nvkm_vram(memory
)->mn
) << NVKM_RAM_MM_SHIFT
;
67 nvkm_vram_addr(struct nvkm_memory
*memory
)
69 struct nvkm_vram
*vram
= nvkm_vram(memory
);
70 if (!nvkm_mm_contiguous(vram
->mn
))
72 return (u64
)nvkm_mm_addr(vram
->mn
) << NVKM_RAM_MM_SHIFT
;
76 nvkm_vram_page(struct nvkm_memory
*memory
)
78 return nvkm_vram(memory
)->page
;
81 static enum nvkm_memory_target
82 nvkm_vram_target(struct nvkm_memory
*memory
)
84 return NVKM_MEM_TARGET_VRAM
;
88 nvkm_vram_dtor(struct nvkm_memory
*memory
)
90 struct nvkm_vram
*vram
= nvkm_vram(memory
);
91 struct nvkm_mm_node
*next
= vram
->mn
;
92 struct nvkm_mm_node
*node
;
93 mutex_lock(&vram
->ram
->fb
->subdev
.mutex
);
94 while ((node
= next
)) {
96 nvkm_mm_free(&vram
->ram
->vram
, &node
);
98 mutex_unlock(&vram
->ram
->fb
->subdev
.mutex
);
102 static const struct nvkm_memory_func
104 .dtor
= nvkm_vram_dtor
,
105 .target
= nvkm_vram_target
,
106 .page
= nvkm_vram_page
,
107 .addr
= nvkm_vram_addr
,
108 .size
= nvkm_vram_size
,
109 .map
= nvkm_vram_map
,
113 nvkm_ram_get(struct nvkm_device
*device
, u8 heap
, u8 type
, u8 rpage
, u64 size
,
114 bool contig
, bool back
, struct nvkm_memory
**pmemory
)
116 struct nvkm_ram
*ram
;
118 struct nvkm_mm_node
**node
, *r
;
119 struct nvkm_vram
*vram
;
120 u8 page
= max(rpage
, (u8
)NVKM_RAM_MM_SHIFT
);
121 u32 align
= (1 << page
) >> NVKM_RAM_MM_SHIFT
;
122 u32 max
= ALIGN(size
, 1 << page
) >> NVKM_RAM_MM_SHIFT
;
123 u32 min
= contig
? max
: align
;
126 if (!device
->fb
|| !(ram
= device
->fb
->ram
))
128 ram
= device
->fb
->ram
;
131 if (!(vram
= kzalloc(sizeof(*vram
), GFP_KERNEL
)))
133 nvkm_memory_ctor(&nvkm_vram
, &vram
->memory
);
136 *pmemory
= &vram
->memory
;
138 mutex_lock(&ram
->fb
->subdev
.mutex
);
142 ret
= nvkm_mm_tail(mm
, heap
, type
, max
, min
, align
, &r
);
144 ret
= nvkm_mm_head(mm
, heap
, type
, max
, min
, align
, &r
);
146 mutex_unlock(&ram
->fb
->subdev
.mutex
);
147 nvkm_memory_unref(pmemory
);
155 mutex_unlock(&ram
->fb
->subdev
.mutex
);
160 nvkm_ram_init(struct nvkm_ram
*ram
)
163 return ram
->func
->init(ram
);
168 nvkm_ram_del(struct nvkm_ram
**pram
)
170 struct nvkm_ram
*ram
= *pram
;
171 if (ram
&& !WARN_ON(!ram
->func
)) {
173 *pram
= ram
->func
->dtor(ram
);
174 nvkm_mm_fini(&ram
->vram
);
181 nvkm_ram_ctor(const struct nvkm_ram_func
*func
, struct nvkm_fb
*fb
,
182 enum nvkm_ram_type type
, u64 size
, struct nvkm_ram
*ram
)
184 static const char *name
[] = {
185 [NVKM_RAM_TYPE_UNKNOWN
] = "of unknown memory type",
186 [NVKM_RAM_TYPE_STOLEN
] = "stolen system memory",
187 [NVKM_RAM_TYPE_SGRAM
] = "SGRAM",
188 [NVKM_RAM_TYPE_SDRAM
] = "SDRAM",
189 [NVKM_RAM_TYPE_DDR1
] = "DDR1",
190 [NVKM_RAM_TYPE_DDR2
] = "DDR2",
191 [NVKM_RAM_TYPE_DDR3
] = "DDR3",
192 [NVKM_RAM_TYPE_GDDR2
] = "GDDR2",
193 [NVKM_RAM_TYPE_GDDR3
] = "GDDR3",
194 [NVKM_RAM_TYPE_GDDR4
] = "GDDR4",
195 [NVKM_RAM_TYPE_GDDR5
] = "GDDR5",
197 struct nvkm_subdev
*subdev
= &fb
->subdev
;
200 nvkm_info(subdev
, "%d MiB %s\n", (int)(size
>> 20), name
[type
]);
206 if (!nvkm_mm_initialised(&ram
->vram
)) {
207 ret
= nvkm_mm_init(&ram
->vram
, NVKM_RAM_MM_NORMAL
, 0,
208 size
>> NVKM_RAM_MM_SHIFT
, 1);
217 nvkm_ram_new_(const struct nvkm_ram_func
*func
, struct nvkm_fb
*fb
,
218 enum nvkm_ram_type type
, u64 size
, struct nvkm_ram
**pram
)
220 if (!(*pram
= kzalloc(sizeof(**pram
), GFP_KERNEL
)))
222 return nvkm_ram_ctor(func
, fb
, type
, size
, *pram
);