1/*
2 * Copyright 2023 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
22#include "priv.h"
23#include "ram.h"
24
25#include <subdev/gsp.h>
26
27static const struct nvkm_ram_func
28r535_fb_ram = {
29};
30
31static int
32r535_fb_ram_new(struct nvkm_fb *fb, struct nvkm_ram **pram)
33{
34	struct nvkm_gsp *gsp = fb->subdev.device->gsp;
35	struct nvkm_ram *ram;
36	int ret;
37
38	if (!(ram = *pram = kzalloc(sizeof(*ram), GFP_KERNEL)))
39		return -ENOMEM;
40
41	ram->func = &r535_fb_ram;
42	ram->fb = fb;
43	ram->type = NVKM_RAM_TYPE_UNKNOWN; /*TODO: pull this from GSP. */
44	ram->size = gsp->fb.size;
45	ram->stolen = false;
46	mutex_init(&ram->mutex);
47
48	for (int i = 0; i < gsp->fb.region_nr; i++) {
49		ret = nvkm_mm_init(&ram->vram, NVKM_RAM_MM_NORMAL,
50				   gsp->fb.region[i].addr >> NVKM_RAM_MM_SHIFT,
51				   gsp->fb.region[i].size >> NVKM_RAM_MM_SHIFT,
52				   1);
53		if (ret)
54			return ret;
55	}
56
57	return 0;
58}
59
60static void *
61r535_fb_dtor(struct nvkm_fb *fb)
62{
63	kfree(fb->func);
64	return fb;
65}
66
67int
68r535_fb_new(const struct nvkm_fb_func *hw,
69	    struct nvkm_device *device, enum nvkm_subdev_type type, int inst, struct nvkm_fb **pfb)
70{
71	struct nvkm_fb_func *rm;
72	int ret;
73
74	if (!(rm = kzalloc(sizeof(*rm), GFP_KERNEL)))
75		return -ENOMEM;
76
77	rm->dtor = r535_fb_dtor;
78	rm->sysmem.flush_page_init = hw->sysmem.flush_page_init;
79	rm->vidmem.size = hw->vidmem.size;
80	rm->ram_new = r535_fb_ram_new;
81
82	ret = nvkm_fb_new_(rm, device, type, inst, pfb);
83	if (ret)
84		kfree(rm);
85
86	return ret;
87}
88