1/*	$NetBSD: nouveau_nvkm_subdev_fb_rammcp77.c,v 1.3 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_rammcp77.c,v 1.3 2021/12/18 23:45:39 riastradh Exp $");
28
29#define mcp77_ram(p) container_of((p), struct mcp77_ram, base)
30#include "ram.h"
31
32struct mcp77_ram {
33	struct nvkm_ram base;
34	u64 poller_base;
35};
36
37static int
38mcp77_ram_init(struct nvkm_ram *base)
39{
40	struct mcp77_ram *ram = mcp77_ram(base);
41	struct nvkm_device *device = ram->base.fb->subdev.device;
42	u32 dniso  = ((ram->base.size - (ram->poller_base + 0x00)) >> 5) - 1;
43	u32 hostnb = ((ram->base.size - (ram->poller_base + 0x20)) >> 5) - 1;
44	u32 flush  = ((ram->base.size - (ram->poller_base + 0x40)) >> 5) - 1;
45
46	/* Enable NISO poller for various clients and set their associated
47	 * read address, only for MCP77/78 and MCP79/7A. (fd#27501)
48	 */
49	nvkm_wr32(device, 0x100c18, dniso);
50	nvkm_mask(device, 0x100c14, 0x00000000, 0x00000001);
51	nvkm_wr32(device, 0x100c1c, hostnb);
52	nvkm_mask(device, 0x100c14, 0x00000000, 0x00000002);
53	nvkm_wr32(device, 0x100c24, flush);
54	nvkm_mask(device, 0x100c14, 0x00000000, 0x00010000);
55	return 0;
56}
57
58static const struct nvkm_ram_func
59mcp77_ram_func = {
60	.init = mcp77_ram_init,
61};
62
63int
64mcp77_ram_new(struct nvkm_fb *fb, struct nvkm_ram **pram)
65{
66	struct nvkm_device *device = fb->subdev.device;
67	u32 rsvd_head = ( 256 * 1024); /* vga memory */
68	u32 rsvd_tail = (1024 * 1024) + 0x1000; /* vbios etc + poller mem */
69	u64 base = (u64)nvkm_rd32(device, 0x100e10) << 12;
70	u64 size = (u64)nvkm_rd32(device, 0x100e14) << 12;
71	struct mcp77_ram *ram;
72	int ret;
73
74	if (!(ram = kzalloc(sizeof(*ram), GFP_KERNEL)))
75		return -ENOMEM;
76	*pram = &ram->base;
77
78	ret = nvkm_ram_ctor(&mcp77_ram_func, fb, NVKM_RAM_TYPE_STOLEN,
79			    size, &ram->base);
80	if (ret)
81		return ret;
82
83	ram->poller_base = size - rsvd_tail;
84	ram->base.stolen = base;
85	nvkm_mm_fini(&ram->base.vram);
86
87	return nvkm_mm_init(&ram->base.vram, NVKM_RAM_MM_NORMAL,
88			    rsvd_head >> NVKM_RAM_MM_SHIFT,
89			    (size - rsvd_head - rsvd_tail) >>
90			    NVKM_RAM_MM_SHIFT, 1);
91}
92