162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2017 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#include <nvif/mem.h>
2362306a36Sopenharmony_ci#include <nvif/client.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <nvif/if000a.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciint
2862306a36Sopenharmony_cinvif_mem_ctor_map(struct nvif_mmu *mmu, const char *name, u8 type, u64 size,
2962306a36Sopenharmony_ci		  struct nvif_mem *mem)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	int ret = nvif_mem_ctor(mmu, name, mmu->mem, NVIF_MEM_MAPPABLE | type,
3262306a36Sopenharmony_ci				0, size, NULL, 0, mem);
3362306a36Sopenharmony_ci	if (ret == 0) {
3462306a36Sopenharmony_ci		ret = nvif_object_map(&mem->object, NULL, 0);
3562306a36Sopenharmony_ci		if (ret)
3662306a36Sopenharmony_ci			nvif_mem_dtor(mem);
3762306a36Sopenharmony_ci	}
3862306a36Sopenharmony_ci	return ret;
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_civoid
4262306a36Sopenharmony_cinvif_mem_dtor(struct nvif_mem *mem)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	nvif_object_dtor(&mem->object);
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciint
4862306a36Sopenharmony_cinvif_mem_ctor_type(struct nvif_mmu *mmu, const char *name, s32 oclass,
4962306a36Sopenharmony_ci		   int type, u8 page, u64 size, void *argv, u32 argc,
5062306a36Sopenharmony_ci		   struct nvif_mem *mem)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	struct nvif_mem_v0 *args;
5362306a36Sopenharmony_ci	u8 stack[128];
5462306a36Sopenharmony_ci	int ret;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	mem->object.client = NULL;
5762306a36Sopenharmony_ci	if (type < 0)
5862306a36Sopenharmony_ci		return -EINVAL;
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	if (sizeof(*args) + argc > sizeof(stack)) {
6162306a36Sopenharmony_ci		if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
6262306a36Sopenharmony_ci			return -ENOMEM;
6362306a36Sopenharmony_ci	} else {
6462306a36Sopenharmony_ci		args = (void *)stack;
6562306a36Sopenharmony_ci	}
6662306a36Sopenharmony_ci	args->version = 0;
6762306a36Sopenharmony_ci	args->type = type;
6862306a36Sopenharmony_ci	args->page = page;
6962306a36Sopenharmony_ci	args->size = size;
7062306a36Sopenharmony_ci	memcpy(args->data, argv, argc);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	ret = nvif_object_ctor(&mmu->object, name ? name : "nvifMem", 0, oclass,
7362306a36Sopenharmony_ci			       args, sizeof(*args) + argc, &mem->object);
7462306a36Sopenharmony_ci	if (ret == 0) {
7562306a36Sopenharmony_ci		mem->type = mmu->type[type].type;
7662306a36Sopenharmony_ci		mem->page = args->page;
7762306a36Sopenharmony_ci		mem->addr = args->addr;
7862306a36Sopenharmony_ci		mem->size = args->size;
7962306a36Sopenharmony_ci	}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	if (args != (void *)stack)
8262306a36Sopenharmony_ci		kfree(args);
8362306a36Sopenharmony_ci	return ret;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciint
8862306a36Sopenharmony_cinvif_mem_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, u8 type,
8962306a36Sopenharmony_ci	      u8 page, u64 size, void *argv, u32 argc, struct nvif_mem *mem)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	int ret = -EINVAL, i;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	mem->object.client = NULL;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	for (i = 0; ret && i < mmu->type_nr; i++) {
9662306a36Sopenharmony_ci		if ((mmu->type[i].type & type) == type) {
9762306a36Sopenharmony_ci			ret = nvif_mem_ctor_type(mmu, name, oclass, i, page,
9862306a36Sopenharmony_ci						 size, argv, argc, mem);
9962306a36Sopenharmony_ci		}
10062306a36Sopenharmony_ci	}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	return ret;
10362306a36Sopenharmony_ci}
104