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