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/vmm.h>
0023 #include <nvif/mem.h>
0024 
0025 #include <nvif/if000c.h>
0026 
0027 int
0028 nvif_vmm_unmap(struct nvif_vmm *vmm, u64 addr)
0029 {
0030     return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_UNMAP,
0031                 &(struct nvif_vmm_unmap_v0) { .addr = addr },
0032                 sizeof(struct nvif_vmm_unmap_v0));
0033 }
0034 
0035 int
0036 nvif_vmm_map(struct nvif_vmm *vmm, u64 addr, u64 size, void *argv, u32 argc,
0037          struct nvif_mem *mem, u64 offset)
0038 {
0039     struct nvif_vmm_map_v0 *args;
0040     u8 stack[48];
0041     int ret;
0042 
0043     if (sizeof(*args) + argc > sizeof(stack)) {
0044         if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
0045             return -ENOMEM;
0046     } else {
0047         args = (void *)stack;
0048     }
0049 
0050     args->version = 0;
0051     args->addr = addr;
0052     args->size = size;
0053     args->memory = nvif_handle(&mem->object);
0054     args->offset = offset;
0055     memcpy(args->data, argv, argc);
0056 
0057     ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_MAP,
0058                    args, sizeof(*args) + argc);
0059     if (args != (void *)stack)
0060         kfree(args);
0061     return ret;
0062 }
0063 
0064 void
0065 nvif_vmm_put(struct nvif_vmm *vmm, struct nvif_vma *vma)
0066 {
0067     if (vma->size) {
0068         WARN_ON(nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PUT,
0069                      &(struct nvif_vmm_put_v0) {
0070                         .addr = vma->addr,
0071                      }, sizeof(struct nvif_vmm_put_v0)));
0072         vma->size = 0;
0073     }
0074 }
0075 
0076 int
0077 nvif_vmm_get(struct nvif_vmm *vmm, enum nvif_vmm_get type, bool sparse,
0078          u8 page, u8 align, u64 size, struct nvif_vma *vma)
0079 {
0080     struct nvif_vmm_get_v0 args;
0081     int ret;
0082 
0083     args.version = vma->size = 0;
0084     args.sparse = sparse;
0085     args.page = page;
0086     args.align = align;
0087     args.size = size;
0088 
0089     switch (type) {
0090     case ADDR: args.type = NVIF_VMM_GET_V0_ADDR; break;
0091     case PTES: args.type = NVIF_VMM_GET_V0_PTES; break;
0092     case LAZY: args.type = NVIF_VMM_GET_V0_LAZY; break;
0093     default:
0094         WARN_ON(1);
0095         return -EINVAL;
0096     }
0097 
0098     ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_GET,
0099                    &args, sizeof(args));
0100     if (ret == 0) {
0101         vma->addr = args.addr;
0102         vma->size = args.size;
0103     }
0104     return ret;
0105 }
0106 
0107 void
0108 nvif_vmm_dtor(struct nvif_vmm *vmm)
0109 {
0110     kfree(vmm->page);
0111     nvif_object_dtor(&vmm->object);
0112 }
0113 
0114 int
0115 nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass, bool managed,
0116           u64 addr, u64 size, void *argv, u32 argc, struct nvif_vmm *vmm)
0117 {
0118     struct nvif_vmm_v0 *args;
0119     u32 argn = sizeof(*args) + argc;
0120     int ret = -ENOSYS, i;
0121 
0122     vmm->object.client = NULL;
0123     vmm->page = NULL;
0124 
0125     if (!(args = kmalloc(argn, GFP_KERNEL)))
0126         return -ENOMEM;
0127     args->version = 0;
0128     args->managed = managed;
0129     args->addr = addr;
0130     args->size = size;
0131     memcpy(args->data, argv, argc);
0132 
0133     ret = nvif_object_ctor(&mmu->object, name ? name : "nvifVmm", 0,
0134                    oclass, args, argn, &vmm->object);
0135     if (ret)
0136         goto done;
0137 
0138     vmm->start = args->addr;
0139     vmm->limit = args->size;
0140 
0141     vmm->page_nr = args->page_nr;
0142     vmm->page = kmalloc_array(vmm->page_nr, sizeof(*vmm->page),
0143                   GFP_KERNEL);
0144     if (!vmm->page) {
0145         ret = -ENOMEM;
0146         goto done;
0147     }
0148 
0149     for (i = 0; i < vmm->page_nr; i++) {
0150         struct nvif_vmm_page_v0 args = { .index = i };
0151 
0152         ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PAGE,
0153                        &args, sizeof(args));
0154         if (ret)
0155             break;
0156 
0157         vmm->page[i].shift = args.shift;
0158         vmm->page[i].sparse = args.sparse;
0159         vmm->page[i].vram = args.vram;
0160         vmm->page[i].host = args.host;
0161         vmm->page[i].comp = args.comp;
0162     }
0163 
0164 done:
0165     if (ret)
0166         nvif_vmm_dtor(vmm);
0167     kfree(args);
0168     return ret;
0169 }