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