Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2017 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  */
0022 #include <nvif/mem.h>
0023 #include <nvif/client.h>
0024 
0025 #include <nvif/if000a.h>
0026 
0027 int
0028 nvif_mem_ctor_map(struct nvif_mmu *mmu, const char *name, u8 type, u64 size,
0029           struct nvif_mem *mem)
0030 {
0031     int ret = nvif_mem_ctor(mmu, name, mmu->mem, NVIF_MEM_MAPPABLE | type,
0032                 0, size, NULL, 0, mem);
0033     if (ret == 0) {
0034         ret = nvif_object_map(&mem->object, NULL, 0);
0035         if (ret)
0036             nvif_mem_dtor(mem);
0037     }
0038     return ret;
0039 }
0040 
0041 void
0042 nvif_mem_dtor(struct nvif_mem *mem)
0043 {
0044     nvif_object_dtor(&mem->object);
0045 }
0046 
0047 int
0048 nvif_mem_ctor_type(struct nvif_mmu *mmu, const char *name, s32 oclass,
0049            int type, u8 page, u64 size, void *argv, u32 argc,
0050            struct nvif_mem *mem)
0051 {
0052     struct nvif_mem_v0 *args;
0053     u8 stack[128];
0054     int ret;
0055 
0056     mem->object.client = NULL;
0057     if (type < 0)
0058         return -EINVAL;
0059 
0060     if (sizeof(*args) + argc > sizeof(stack)) {
0061         if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
0062             return -ENOMEM;
0063     } else {
0064         args = (void *)stack;
0065     }
0066     args->version = 0;
0067     args->type = type;
0068     args->page = page;
0069     args->size = size;
0070     memcpy(args->data, argv, argc);
0071 
0072     ret = nvif_object_ctor(&mmu->object, name ? name : "nvifMem", 0, oclass,
0073                    args, sizeof(*args) + argc, &mem->object);
0074     if (ret == 0) {
0075         mem->type = mmu->type[type].type;
0076         mem->page = args->page;
0077         mem->addr = args->addr;
0078         mem->size = args->size;
0079     }
0080 
0081     if (args != (void *)stack)
0082         kfree(args);
0083     return ret;
0084 
0085 }
0086 
0087 int
0088 nvif_mem_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, u8 type,
0089           u8 page, u64 size, void *argv, u32 argc, struct nvif_mem *mem)
0090 {
0091     int ret = -EINVAL, i;
0092 
0093     mem->object.client = NULL;
0094 
0095     for (i = 0; ret && i < mmu->type_nr; i++) {
0096         if ((mmu->type[i].type & type) == type) {
0097             ret = nvif_mem_ctor_type(mmu, name, oclass, i, page,
0098                          size, argv, argc, mem);
0099         }
0100     }
0101 
0102     return ret;
0103 }