1*677dec6eSriastradh /*	$NetBSD: nouveau_nvkm_subdev_bios_shadowramin.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $	*/
2d350ecf5Sriastradh 
3d350ecf5Sriastradh /*
4d350ecf5Sriastradh  * Copyright 2012 Red Hat Inc.
5d350ecf5Sriastradh  *
6d350ecf5Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
7d350ecf5Sriastradh  * copy of this software and associated documentation files (the "Software"),
8d350ecf5Sriastradh  * to deal in the Software without restriction, including without limitation
9d350ecf5Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10d350ecf5Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
11d350ecf5Sriastradh  * Software is furnished to do so, subject to the following conditions:
12d350ecf5Sriastradh  *
13d350ecf5Sriastradh  * The above copyright notice and this permission notice shall be included in
14d350ecf5Sriastradh  * all copies or substantial portions of the Software.
15d350ecf5Sriastradh  *
16d350ecf5Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d350ecf5Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d350ecf5Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19d350ecf5Sriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20d350ecf5Sriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21d350ecf5Sriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22d350ecf5Sriastradh  * OTHER DEALINGS IN THE SOFTWARE.
23d350ecf5Sriastradh  *
24d350ecf5Sriastradh  */
25d350ecf5Sriastradh #include <sys/cdefs.h>
26*677dec6eSriastradh __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_shadowramin.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
27d350ecf5Sriastradh 
28d350ecf5Sriastradh #include "priv.h"
29d350ecf5Sriastradh 
30d350ecf5Sriastradh struct priv {
31d350ecf5Sriastradh 	struct nvkm_bios *bios;
32d350ecf5Sriastradh 	u32 bar0;
33d350ecf5Sriastradh };
34d350ecf5Sriastradh 
35d350ecf5Sriastradh static u32
pramin_read(void * data,u32 offset,u32 length,struct nvkm_bios * bios)36d350ecf5Sriastradh pramin_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
37d350ecf5Sriastradh {
38d350ecf5Sriastradh 	struct nvkm_device *device = bios->subdev.device;
39d350ecf5Sriastradh 	u32 i;
40d350ecf5Sriastradh 	if (offset + length <= 0x00100000) {
41d350ecf5Sriastradh 		for (i = offset; i < offset + length; i += 4)
42d350ecf5Sriastradh 			*(u32 *)&bios->data[i] = nvkm_rd32(device, 0x700000 + i);
43d350ecf5Sriastradh 		return length;
44d350ecf5Sriastradh 	}
45d350ecf5Sriastradh 	return 0;
46d350ecf5Sriastradh }
47d350ecf5Sriastradh 
48d350ecf5Sriastradh static void
pramin_fini(void * data)49d350ecf5Sriastradh pramin_fini(void *data)
50d350ecf5Sriastradh {
51d350ecf5Sriastradh 	struct priv *priv = data;
52d350ecf5Sriastradh 	if (priv) {
53d350ecf5Sriastradh 		struct nvkm_device *device = priv->bios->subdev.device;
54d350ecf5Sriastradh 		nvkm_wr32(device, 0x001700, priv->bar0);
55d350ecf5Sriastradh 		kfree(priv);
56d350ecf5Sriastradh 	}
57d350ecf5Sriastradh }
58d350ecf5Sriastradh 
59d350ecf5Sriastradh static void *
pramin_init(struct nvkm_bios * bios,const char * name)60d350ecf5Sriastradh pramin_init(struct nvkm_bios *bios, const char *name)
61d350ecf5Sriastradh {
62d350ecf5Sriastradh 	struct nvkm_subdev *subdev = &bios->subdev;
63d350ecf5Sriastradh 	struct nvkm_device *device = subdev->device;
64d350ecf5Sriastradh 	struct priv *priv = NULL;
65d350ecf5Sriastradh 	u64 addr = 0;
66d350ecf5Sriastradh 
67d350ecf5Sriastradh 	/* PRAMIN always potentially available prior to nv50 */
68d350ecf5Sriastradh 	if (device->card_type < NV_50)
69d350ecf5Sriastradh 		return NULL;
70d350ecf5Sriastradh 
71d350ecf5Sriastradh 	/* we can't get the bios image pointer without PDISP */
72d350ecf5Sriastradh 	if (device->card_type >= GM100)
73d350ecf5Sriastradh 		addr = nvkm_rd32(device, 0x021c04);
74d350ecf5Sriastradh 	else
75d350ecf5Sriastradh 	if (device->card_type >= NV_C0)
76d350ecf5Sriastradh 		addr = nvkm_rd32(device, 0x022500);
77d350ecf5Sriastradh 	if (addr & 0x00000001) {
78d350ecf5Sriastradh 		nvkm_debug(subdev, "... display disabled\n");
79d350ecf5Sriastradh 		return ERR_PTR(-ENODEV);
80d350ecf5Sriastradh 	}
81d350ecf5Sriastradh 
82d350ecf5Sriastradh 	/* check that the window is enabled and in vram, particularly
83d350ecf5Sriastradh 	 * important as we don't want to be touching vram on an
84d350ecf5Sriastradh 	 * uninitialised board
85d350ecf5Sriastradh 	 */
86*677dec6eSriastradh 	if (device->card_type >= GV100)
87*677dec6eSriastradh 		addr = nvkm_rd32(device, 0x625f04);
88*677dec6eSriastradh 	else
89d350ecf5Sriastradh 		addr = nvkm_rd32(device, 0x619f04);
90d350ecf5Sriastradh 	if (!(addr & 0x00000008)) {
91d350ecf5Sriastradh 		nvkm_debug(subdev, "... not enabled\n");
92d350ecf5Sriastradh 		return ERR_PTR(-ENODEV);
93d350ecf5Sriastradh 	}
94d350ecf5Sriastradh 	if ( (addr & 0x00000003) != 1) {
95d350ecf5Sriastradh 		nvkm_debug(subdev, "... not in vram\n");
96d350ecf5Sriastradh 		return ERR_PTR(-ENODEV);
97d350ecf5Sriastradh 	}
98d350ecf5Sriastradh 
99d350ecf5Sriastradh 	/* some alternate method inherited from xf86-video-nv... */
100d350ecf5Sriastradh 	addr = (addr & 0xffffff00) << 8;
101d350ecf5Sriastradh 	if (!addr) {
102d350ecf5Sriastradh 		addr  = (u64)nvkm_rd32(device, 0x001700) << 16;
103d350ecf5Sriastradh 		addr += 0xf0000;
104d350ecf5Sriastradh 	}
105d350ecf5Sriastradh 
106d350ecf5Sriastradh 	/* modify bar0 PRAMIN window to cover the bios image */
107d350ecf5Sriastradh 	if (!(priv = kmalloc(sizeof(*priv), GFP_KERNEL))) {
108d350ecf5Sriastradh 		nvkm_error(subdev, "... out of memory\n");
109d350ecf5Sriastradh 		return ERR_PTR(-ENOMEM);
110d350ecf5Sriastradh 	}
111d350ecf5Sriastradh 
112d350ecf5Sriastradh 	priv->bios = bios;
113d350ecf5Sriastradh 	priv->bar0 = nvkm_rd32(device, 0x001700);
114d350ecf5Sriastradh 	nvkm_wr32(device, 0x001700, addr >> 16);
115d350ecf5Sriastradh 	return priv;
116d350ecf5Sriastradh }
117d350ecf5Sriastradh 
118d350ecf5Sriastradh const struct nvbios_source
119d350ecf5Sriastradh nvbios_ramin = {
120d350ecf5Sriastradh 	.name = "PRAMIN",
121d350ecf5Sriastradh 	.init = pramin_init,
122d350ecf5Sriastradh 	.fini = pramin_fini,
123d350ecf5Sriastradh 	.read = pramin_read,
124d350ecf5Sriastradh 	.rw = true,
125d350ecf5Sriastradh };
126