1/*	$NetBSD: nouveau_nvkm_subdev_fb_ramgp100.c,v 1.2 2021/12/18 23:45:39 riastradh Exp $	*/
2
3/*
4 * Copyright 2013 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 * Authors: Ben Skeggs
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_fb_ramgp100.c,v 1.2 2021/12/18 23:45:39 riastradh Exp $");
28
29#include "ram.h"
30
31#include <subdev/bios.h>
32#include <subdev/bios/init.h>
33#include <subdev/bios/rammap.h>
34
35static int
36gp100_ram_init(struct nvkm_ram *ram)
37{
38	struct nvkm_subdev *subdev = &ram->fb->subdev;
39	struct nvkm_device *device = subdev->device;
40	struct nvkm_bios *bios = device->bios;
41	u8  ver, hdr, cnt, len, snr, ssz;
42	u32 data;
43	int i;
44
45	/* run a bunch of tables from rammap table.  there's actually
46	 * individual pointers for each rammap entry too, but, nvidia
47	 * seem to just run the last two entries' scripts early on in
48	 * their init, and never again.. we'll just run 'em all once
49	 * for now.
50	 *
51	 * i strongly suspect that each script is for a separate mode
52	 * (likely selected by 0x9a065c's lower bits?), and the
53	 * binary driver skips the one that's already been setup by
54	 * the init tables.
55	 */
56	data = nvbios_rammapTe(bios, &ver, &hdr, &cnt, &len, &snr, &ssz);
57	if (!data || hdr < 0x15)
58		return -EINVAL;
59
60	cnt  = nvbios_rd08(bios, data + 0x14); /* guess at count */
61	data = nvbios_rd32(bios, data + 0x10); /* guess u32... */
62	if (cnt) {
63		u32 save = nvkm_rd32(device, 0x9a065c) & 0x000000f0;
64		for (i = 0; i < cnt; i++, data += 4) {
65			if (i != save >> 4) {
66				nvkm_mask(device, 0x9a065c, 0x000000f0, i << 4);
67				nvbios_init(subdev, nvbios_rd32(bios, data));
68			}
69		}
70		nvkm_mask(device, 0x9a065c, 0x000000f0, save);
71	}
72
73	nvkm_mask(device, 0x9a0584, 0x11000000, 0x00000000);
74	nvkm_wr32(device, 0x10ecc0, 0xffffffff);
75	nvkm_mask(device, 0x9a0160, 0x00000010, 0x00000010);
76	return 0;
77}
78
79static u32
80gp100_ram_probe_fbpa(struct nvkm_device *device, int fbpa)
81{
82	return nvkm_rd32(device, 0x90020c + (fbpa * 0x4000));
83}
84
85static const struct nvkm_ram_func
86gp100_ram = {
87	.upper = 0x1000000000ULL,
88	.probe_fbp = gm107_ram_probe_fbp,
89	.probe_fbp_amount = gm200_ram_probe_fbp_amount,
90	.probe_fbpa_amount = gp100_ram_probe_fbpa,
91	.init = gp100_ram_init,
92};
93
94int
95gp100_ram_new(struct nvkm_fb *fb, struct nvkm_ram **pram)
96{
97	struct nvkm_ram *ram;
98
99	if (!(ram = *pram = kzalloc(sizeof(*ram), GFP_KERNEL)))
100		return -ENOMEM;
101
102	return gf100_ram_ctor(&gp100_ram, fb, ram);
103
104}
105