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