Annotation of src/sys/dev/hyperv/hyperv_common.c, revision 1.4

1.4     ! nonaka      1: /*     $NetBSD: hyperv_common.c,v 1.3 2019/12/06 12:46:06 nonaka Exp $ */
1.1       nonaka      2: 
                      3: /*-
                      4:  * Copyright (c) 2009-2012,2016-2017 Microsoft Corp.
                      5:  * Copyright (c) 2012 NetApp Inc.
                      6:  * Copyright (c) 2012 Citrix Inc.
                      7:  * All rights reserved.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice unmodified, this list of conditions, and the following
                     14:  *    disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  */
                     30: 
                     31: #include <sys/cdefs.h>
1.4     ! nonaka     32: __KERNEL_RCSID(0, "$NetBSD: hyperv_common.c,v 1.3 2019/12/06 12:46:06 nonaka Exp $");
1.1       nonaka     33: 
                     34: #include "hyperv.h"
                     35: 
                     36: #include <sys/param.h>
                     37: #include <sys/systm.h>
                     38: #include <sys/kernel.h>
                     39: #include <sys/bus.h>
                     40: #include <sys/kmem.h>
                     41: 
                     42: #include <dev/hyperv/hypervreg.h>
                     43: #include <dev/hyperv/hypervvar.h>
                     44: 
                     45: hyperv_tc64_t hyperv_tc64;
                     46: 
                     47: int            hyperv_nullop(void);
                     48: void           hyperv_voidop(void);
                     49: uint64_t       hyperv_hypercall_error(uint64_t, paddr_t, paddr_t);
                     50: 
                     51: __weak_alias(hyperv_hypercall, hyperv_hypercall_error);
                     52: __weak_alias(hyperv_hypercall_enabled, hyperv_nullop);
                     53: __weak_alias(hyperv_synic_supported, hyperv_nullop);
1.2       nonaka     54: __weak_alias(hyperv_is_gen1, hyperv_nullop);
1.1       nonaka     55: __weak_alias(hyperv_set_event_proc, hyperv_voidop);
                     56: __weak_alias(hyperv_set_message_proc, hyperv_voidop);
                     57: __weak_alias(hyperv_send_eom, hyperv_voidop);
                     58: __weak_alias(hyperv_intr, hyperv_voidop);
1.4     ! nonaka     59: __weak_alias(hyperv_get_vcpuid, hyperv_nullop);
1.1       nonaka     60: __weak_alias(vmbus_init_interrupts_md, hyperv_voidop);
                     61: __weak_alias(vmbus_deinit_interrupts_md, hyperv_voidop);
                     62: __weak_alias(vmbus_init_synic_md, hyperv_voidop);
                     63: __weak_alias(vmbus_deinit_synic_md, hyperv_voidop);
                     64: 
                     65: int
                     66: hyperv_nullop(void)
                     67: {
                     68:        return 0;
                     69: }
                     70: 
                     71: void
                     72: hyperv_voidop(void)
                     73: {
                     74: }
                     75: 
                     76: uint64_t
                     77: hyperv_hypercall_error(uint64_t control, paddr_t in_paddr, paddr_t out_paddr)
                     78: {
                     79:        return ~HYPERCALL_STATUS_SUCCESS;
                     80: }
                     81: 
                     82: uint64_t
                     83: hyperv_hypercall_post_message(paddr_t msg)
                     84: {
                     85: 
                     86:        return hyperv_hypercall(HYPERCALL_POST_MESSAGE, msg, 0);
                     87: }
                     88: 
                     89: uint64_t
                     90: hyperv_hypercall_signal_event(paddr_t monprm)
                     91: {
                     92: 
                     93:        return hyperv_hypercall(HYPERCALL_SIGNAL_EVENT, monprm, 0);
                     94: }
                     95: 
                     96: int
                     97: hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
                     98: {
                     99:        const uint8_t *d = guid->hv_guid;
                    100: 
                    101:        return snprintf(buf, sz, "%02x%02x%02x%02x-"
                    102:            "%02x%02x-%02x%02x-%02x%02x-"
                    103:            "%02x%02x%02x%02x%02x%02x",
                    104:            d[3], d[2], d[1], d[0],
                    105:            d[5], d[4], d[7], d[6], d[8], d[9],
                    106:            d[10], d[11], d[12], d[13], d[14], d[15]);
                    107: }
                    108: 
                    109: /*
                    110:  * Hyper-V bus_dma utilities.
                    111:  */
                    112: void *
                    113: hyperv_dma_alloc(bus_dma_tag_t dmat, struct hyperv_dma *dma, bus_size_t size,
                    114:     bus_size_t alignment, bus_size_t boundary, int nsegs)
                    115: {
                    116:        const int kmemflags = cold ? KM_NOSLEEP : KM_SLEEP;
                    117:        const int dmaflags = cold ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK;
                    118:        int rseg, error;
                    119: 
                    120:        KASSERT(dma != NULL);
                    121:        KASSERT(dma->segs == NULL);
                    122:        KASSERT(nsegs > 0);
                    123: 
                    124:        dma->segs = kmem_zalloc(sizeof(*dma->segs) * nsegs, kmemflags);
                    125:        if (dma->segs == NULL)
                    126:                return NULL;
                    127: 
                    128:        dma->nsegs = nsegs;
                    129: 
                    130:        error = bus_dmamem_alloc(dmat, size, alignment, boundary, dma->segs,
                    131:            nsegs, &rseg, dmaflags);
                    132:        if (error) {
                    133:                printf("%s: bus_dmamem_alloc failed: error=%d\n",
                    134:                    __func__, error);
                    135:                goto fail1;
                    136:        }
                    137:        error = bus_dmamem_map(dmat, dma->segs, rseg, size, &dma->addr,
                    138:            dmaflags);
                    139:        if (error) {
                    140:                printf("%s: bus_dmamem_map failed: error=%d\n",
                    141:                    __func__, error);
                    142:                goto fail2;
                    143:        }
                    144:        error = bus_dmamap_create(dmat, size, rseg, size, boundary, dmaflags,
                    145:            &dma->map);
                    146:        if (error) {
                    147:                printf("%s: bus_dmamap_create failed: error=%d\n",
                    148:                    __func__, error);
                    149:                goto fail3;
                    150:        }
                    151:        error = bus_dmamap_load(dmat, dma->map, dma->addr, size, NULL,
                    152:            BUS_DMA_READ | BUS_DMA_WRITE | dmaflags);
                    153:        if (error) {
                    154:                printf("%s: bus_dmamap_load failed: error=%d\n",
                    155:                    __func__, error);
                    156:                goto fail4;
                    157:        }
                    158: 
1.3       nonaka    159:        memset(dma->addr, 0, dma->map->dm_mapsize);
                    160:        bus_dmamap_sync(dmat, dma->map, 0, dma->map->dm_mapsize,
                    161:            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
                    162: 
1.1       nonaka    163:        return dma->addr;
                    164: 
                    165: fail4: bus_dmamap_destroy(dmat, dma->map);
                    166: fail3: bus_dmamem_unmap(dmat, dma->addr, size);
                    167:        dma->addr = NULL;
                    168: fail2: bus_dmamem_free(dmat, dma->segs, rseg);
                    169: fail1: kmem_free(dma->segs, sizeof(*dma->segs) * nsegs);
                    170:        dma->segs = NULL;
                    171:        dma->nsegs = 0;
                    172:        return NULL;
                    173: }
                    174: 
                    175: void
                    176: hyperv_dma_free(bus_dma_tag_t dmat, struct hyperv_dma *dma)
                    177: {
                    178:        bus_size_t size = dma->map->dm_mapsize;
                    179:        int rsegs = dma->map->dm_nsegs;
                    180: 
                    181:        bus_dmamap_unload(dmat, dma->map);
                    182:        bus_dmamap_destroy(dmat, dma->map);
                    183:        bus_dmamem_unmap(dmat, dma->addr, size);
                    184:        dma->addr = NULL;
                    185:        bus_dmamem_free(dmat, dma->segs, rsegs);
                    186:        kmem_free(dma->segs, sizeof(*dma->segs) * dma->nsegs);
                    187:        dma->segs = NULL;
                    188:        dma->nsegs = 0;
                    189: }

CVSweb <webmaster@jp.NetBSD.org>