1/*	$NetBSD: nouveau_nvkm_subdev_bios_shadowramin.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $	*/
2
3/*
4 * Copyright 2012 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 */
25#include <sys/cdefs.h>
26__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_shadowramin.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
27
28#include "priv.h"
29
30struct priv {
31	struct nvkm_bios *bios;
32	u32 bar0;
33};
34
35static u32
36pramin_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
37{
38	struct nvkm_device *device = bios->subdev.device;
39	u32 i;
40	if (offset + length <= 0x00100000) {
41		for (i = offset; i < offset + length; i += 4)
42			*(u32 *)&bios->data[i] = nvkm_rd32(device, 0x700000 + i);
43		return length;
44	}
45	return 0;
46}
47
48static void
49pramin_fini(void *data)
50{
51	struct priv *priv = data;
52	if (priv) {
53		struct nvkm_device *device = priv->bios->subdev.device;
54		nvkm_wr32(device, 0x001700, priv->bar0);
55		kfree(priv);
56	}
57}
58
59static void *
60pramin_init(struct nvkm_bios *bios, const char *name)
61{
62	struct nvkm_subdev *subdev = &bios->subdev;
63	struct nvkm_device *device = subdev->device;
64	struct priv *priv = NULL;
65	u64 addr = 0;
66
67	/* PRAMIN always potentially available prior to nv50 */
68	if (device->card_type < NV_50)
69		return NULL;
70
71	/* we can't get the bios image pointer without PDISP */
72	if (device->card_type >= GM100)
73		addr = nvkm_rd32(device, 0x021c04);
74	else
75	if (device->card_type >= NV_C0)
76		addr = nvkm_rd32(device, 0x022500);
77	if (addr & 0x00000001) {
78		nvkm_debug(subdev, "... display disabled\n");
79		return ERR_PTR(-ENODEV);
80	}
81
82	/* check that the window is enabled and in vram, particularly
83	 * important as we don't want to be touching vram on an
84	 * uninitialised board
85	 */
86	if (device->card_type >= GV100)
87		addr = nvkm_rd32(device, 0x625f04);
88	else
89		addr = nvkm_rd32(device, 0x619f04);
90	if (!(addr & 0x00000008)) {
91		nvkm_debug(subdev, "... not enabled\n");
92		return ERR_PTR(-ENODEV);
93	}
94	if ( (addr & 0x00000003) != 1) {
95		nvkm_debug(subdev, "... not in vram\n");
96		return ERR_PTR(-ENODEV);
97	}
98
99	/* some alternate method inherited from xf86-video-nv... */
100	addr = (addr & 0xffffff00) << 8;
101	if (!addr) {
102		addr  = (u64)nvkm_rd32(device, 0x001700) << 16;
103		addr += 0xf0000;
104	}
105
106	/* modify bar0 PRAMIN window to cover the bios image */
107	if (!(priv = kmalloc(sizeof(*priv), GFP_KERNEL))) {
108		nvkm_error(subdev, "... out of memory\n");
109		return ERR_PTR(-ENOMEM);
110	}
111
112	priv->bios = bios;
113	priv->bar0 = nvkm_rd32(device, 0x001700);
114	nvkm_wr32(device, 0x001700, addr >> 16);
115	return priv;
116}
117
118const struct nvbios_source
119nvbios_ramin = {
120	.name = "PRAMIN",
121	.init = pramin_init,
122	.fini = pramin_fini,
123	.read = pramin_read,
124	.rw = true,
125};
126