1121934Sharti/*	$NetBSD: nouveau_nvkm_subdev_mmu_memnv50.c,v 1.3 2021/12/19 10:51:58 riastradh Exp $	*/
2121934Sharti
3121934Sharti/*
4121934Sharti * Copyright 2017 Red Hat Inc.
5121934Sharti *
6121934Sharti * Permission is hereby granted, free of charge, to any person obtaining a
7121934Sharti * copy of this software and associated documentation files (the "Software"),
8121934Sharti * to deal in the Software without restriction, including without limitation
9121934Sharti * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10121934Sharti * and/or sell copies of the Software, and to permit persons to whom the
11121934Sharti * Software is furnished to do so, subject to the following conditions:
12121934Sharti *
13121934Sharti * The above copyright notice and this permission notice shall be included in
14121934Sharti * all copies or substantial portions of the Software.
15121934Sharti *
16121934Sharti * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17121934Sharti * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18121934Sharti * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19121934Sharti * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20121934Sharti * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21121934Sharti * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22121934Sharti * OTHER DEALINGS IN THE SOFTWARE.
23121934Sharti */
24121934Sharti#include <sys/cdefs.h>
25121934Sharti__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_mmu_memnv50.c,v 1.3 2021/12/19 10:51:58 riastradh Exp $");
26121934Sharti
27121934Sharti#include "mem.h"
28121934Sharti
29131826Sharti#include <core/memory.h>
30121934Sharti#include <subdev/bar.h>
31121934Sharti#include <subdev/fb.h>
32121934Sharti
33121934Sharti#include <nvif/class.h>
34121934Sharti#include <nvif/if500b.h>
35121934Sharti#include <nvif/if500d.h>
36121934Sharti#include <nvif/unpack.h>
37121934Sharti
38121934Shartiint
39121934Sharti#ifdef __NetBSD__
40121934Shartinv50_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
41121934Sharti	     u32 argc, bus_space_tag_t *ptag, u64 *paddr, u64 *psize, struct nvkm_vma **pvma)
42121934Sharti#else
43121934Shartinv50_mem_map(struct nvkm_mmu *mmu, struct nvkm_memory *memory, void *argv,
44121934Sharti	     u32 argc, u64 *paddr, u64 *psize, struct nvkm_vma **pvma)
45121934Sharti#endif
46121934Sharti{
47121934Sharti	struct nv50_vmm_map_v0 uvmm = {};
48121934Sharti	union {
49121934Sharti		struct nv50_mem_map_vn vn;
50121934Sharti		struct nv50_mem_map_v0 v0;
51121934Sharti	} *args = argv;
52121934Sharti	struct nvkm_device *device = mmu->subdev.device;
53121934Sharti	struct nvkm_vmm *bar = nvkm_bar_bar1_vmm(device);
54121934Sharti	u64 size = nvkm_memory_size(memory);
55121934Sharti	int ret = -ENOSYS;
56121934Sharti
57121934Sharti	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
58121934Sharti		uvmm.ro   = args->v0.ro;
59121934Sharti		uvmm.kind = args->v0.kind;
60121934Sharti		uvmm.comp = args->v0.comp;
61121934Sharti	} else
62121934Sharti	if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
63121934Sharti	} else
64121934Sharti		return ret;
65121934Sharti
66121934Sharti	ret = nvkm_vmm_get(bar, 12, size, pvma);
67121934Sharti	if (ret)
68121934Sharti		return ret;
69121934Sharti
70121934Sharti#ifdef __NetBSD__
71121934Sharti	*ptag = device->func->resource_tag(device, 1);
72121934Sharti#endif
73121934Sharti	*paddr = device->func->resource_addr(device, 1) + (*pvma)->addr;
74121934Sharti	*psize = (*pvma)->size;
75121934Sharti	return nvkm_memory_map(memory, 0, bar, *pvma, &uvmm, sizeof(uvmm));
76121934Sharti}
77121934Sharti
78121934Shartiint
79121934Shartinv50_mem_new(struct nvkm_mmu *mmu, int type, u8 page, u64 size,
80121934Sharti	     void *argv, u32 argc, struct nvkm_memory **pmemory)
81121934Sharti{
82121934Sharti	union {
83121934Sharti		struct nv50_mem_vn vn;
84121934Sharti		struct nv50_mem_v0 v0;
85121934Sharti	} *args = argv;
86121934Sharti	int ret = -ENOSYS;
87121934Sharti	bool contig;
88121934Sharti
89121934Sharti	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
90121934Sharti		type   = args->v0.bankswz ? 0x02 : 0x01;
91121934Sharti		contig = args->v0.contig;
92121934Sharti	} else
93121934Sharti	if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
94121934Sharti		type   = 0x01;
95121934Sharti		contig = false;
96121934Sharti	} else
97121934Sharti		return -ENOSYS;
98121934Sharti
99121934Sharti	return nvkm_ram_get(mmu->subdev.device, NVKM_RAM_MM_NORMAL, type,
100121934Sharti			    page, size, contig, false, pmemory);
101121934Sharti}
102121934Sharti