The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sun4v/mdesc/mdesc_init.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2006 Kip Macy
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #include <sys/types.h>
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/bus.h>
   35 #include <sys/kernel.h>
   36 #include <sys/lock.h>
   37 #include <sys/mutex.h>
   38 #include <vm/vm.h>
   39 #include <vm/pmap.h>
   40 
   41 #include <machine/hypervisorvar.h>
   42 #include <machine/hv_api.h>
   43 #include <machine/cddl/mdesc.h>
   44 #include <machine/cddl/mdesc_impl.h>
   45 
   46 
   47 static void mdesc_postvm_init(void *);
   48 /*
   49  * It doesn't really matter when this happens right now
   50  * it just needs to happen before device initialization 
   51  * and after VM initialization. Later on we will end up 
   52  * doing this statically from pmap_bootstrap so that we 
   53  * can kill off all calls to OBP. OBP removal is not in
   54  * the critical path for sun4v at this time.
   55  */
   56 SYSINIT(mdesc_init, SI_SUB_CPU, SI_ORDER_SECOND, mdesc_postvm_init, NULL);
   57 
   58 
   59 #define UNIMPLEMENTED panic("%s not implemented.", __FUNCTION__)
   60 
   61 typedef struct mdesc_memops_ {
   62         void            *(*mm_buf_alloc)(size_t size, size_t align);
   63         void            (*mm_buf_free)(void *, size_t size);
   64         void            *(*mm_meta_alloc)(size_t size);
   65         void            (*mm_meta_free)(void *, size_t size);
   66 } mdesc_memops_t;
   67 
   68 typedef struct mdesc_ {
   69         uint64_t        md_gen;         /* md-generation# */
   70         struct mtx      md_lock;       
   71         void           *md_addr;        /* address of raw MD */
   72         uint64_t        md_size;        /* size of raw MD */
   73         uint64_t        md_buf_size;    /* size of buffer allocated for MD */
   74         int             md_refcount;    /* reference count */
   75         mdesc_memops_t *md_memops;      /* Memory operations for this MD */
   76 } mdesc_t;
   77 
   78 
   79 
   80 static mdesc_t        *curr_mdesc = NULL;
   81 static struct mtx      curr_mdesc_lock;
   82 static mdesc_memops_t *mdesc_memops;
   83 
   84 static void *mdesc_boot_buf_alloc(size_t size, size_t align);
   85 static void mdesc_boot_buf_free(void *buf, size_t align);
   86 static void *mdesc_boot_meta_alloc(size_t size);
   87 static void mdesc_boot_meta_free(void *buf, size_t size);
   88 
   89 static void *mdesc_buf_alloc(size_t size, size_t align);
   90 static void mdesc_buf_free(void *buf, size_t align);
   91 static void *mdesc_meta_alloc(size_t size);
   92 static void mdesc_meta_free(void *buf, size_t size);
   93 
   94 static mdesc_memops_t mdesc_boot_memops = {
   95         mdesc_boot_buf_alloc,
   96         mdesc_boot_buf_free,
   97         mdesc_boot_meta_alloc,
   98         mdesc_boot_meta_free,
   99 };
  100 
  101 static mdesc_memops_t mdesc_generic_memops = {
  102         mdesc_buf_alloc,
  103         mdesc_buf_free,
  104         mdesc_meta_alloc,
  105         mdesc_meta_free,
  106 };
  107 
  108 static void *
  109 mdesc_boot_buf_alloc(size_t size, size_t align)
  110 {
  111         UNIMPLEMENTED;
  112 }
  113 
  114 static void 
  115 mdesc_boot_buf_free(void *buf, size_t align)
  116 {
  117         UNIMPLEMENTED;
  118 }
  119 
  120 static void *
  121 mdesc_boot_meta_alloc(size_t size)
  122 {
  123         UNIMPLEMENTED;
  124 }
  125 
  126 static void 
  127 mdesc_boot_meta_free(void *buf, size_t size)
  128 {
  129         UNIMPLEMENTED;
  130 }
  131 
  132 static void *
  133 mdesc_buf_alloc(size_t size, size_t align)
  134 {
  135         return pmap_alloc_zeroed_contig_pages(size/PAGE_SIZE, align);
  136 }
  137 
  138 static void 
  139 mdesc_buf_free(void *buf, size_t align)
  140 {
  141         contigfree(buf, PAGE_SIZE /*fix me*/, M_MDPROP);
  142 }
  143 
  144 static void *
  145 mdesc_meta_alloc(size_t size)
  146 {
  147         return malloc(size, M_MDPROP, M_NOWAIT);
  148 }
  149 
  150 static void 
  151 mdesc_meta_free(void *buf, size_t size)
  152 {
  153         free(buf, M_MDPROP);
  154 }
  155 
  156 void 
  157 mdesc_init(void)
  158 {
  159 
  160         mtx_init(&curr_mdesc_lock, "current machine description lock", NULL, MTX_DEF);
  161         mdesc_memops = &mdesc_boot_memops;
  162 }
  163 
  164 static void 
  165 mdesc_postvm_init(void *unused)
  166 {
  167         mdesc_memops = &mdesc_generic_memops;
  168 }
  169 
  170 static mdesc_t *
  171 mdesc_alloc(void)
  172 {
  173         mdesc_t *mdp;
  174         
  175         mdp = (mdesc_t *)(mdesc_memops->mm_meta_alloc)(sizeof(mdesc_t));
  176         if (mdp != NULL) {
  177                 bzero(mdp, sizeof(*mdp));
  178                 mdp->md_memops = mdesc_memops;
  179                 mtx_init(&mdp->md_lock, "machine descriptor lock", NULL, MTX_DEF);
  180         }
  181         
  182         return (mdp);
  183 }
  184 
  185 int 
  186 mdesc_update(void)
  187 {
  188         uint64_t rv;
  189         uint64_t mdesc_size, mdesc_buf_size = 0;
  190         void    *buf = NULL;
  191 #ifdef notyet
  192         uint64_t gen;
  193 #endif
  194         do {
  195                 if (buf != NULL)
  196                         (mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);
  197 
  198                 mdesc_size = 0LL;
  199                 do {
  200                         rv = hv_mach_desc((uint64_t)0, &mdesc_size);
  201                         if (rv != H_EOK && rv != H_EINVAL)
  202                                 printf("retrying to fetch mdesc size\n");
  203                 } while (rv != H_EOK && rv != H_EINVAL); 
  204 
  205                 mdesc_size = mdesc_buf_size = round_page(mdesc_size);
  206                 
  207                 if ((buf = (*mdesc_memops->mm_buf_alloc)(mdesc_buf_size, PAGE_SIZE)) == NULL) {
  208                         rv = ENOMEM;
  209                         goto done;
  210                 }
  211                 
  212                 rv = hv_mach_desc(vtophys(buf), &mdesc_size);
  213 
  214                 if (rv != H_EOK && rv != H_EINVAL) {
  215                         goto done;
  216                 }
  217         } while (mdesc_size > mdesc_buf_size);
  218         
  219         KASSERT(rv == H_EOK, ("unexpected return from hv_mach_desc"));
  220         
  221         /* XXX we ignore the generation count... not all versions may 
  222          * support it  
  223          */
  224 
  225         if (curr_mdesc->md_refcount == 0) {
  226                 (*mdesc_memops->mm_buf_free) (curr_mdesc->md_addr, curr_mdesc->md_size);
  227         } else {
  228                 panic("out of date machine description list not implemented");
  229         }
  230 
  231         mtx_lock(&curr_mdesc_lock);
  232 #ifdef notyet
  233         curr_mdesc->md_gen = gen;
  234 #endif
  235         curr_mdesc->md_addr = buf;
  236         curr_mdesc->md_size = mdesc_size;
  237         curr_mdesc->md_buf_size = mdesc_buf_size;
  238         mtx_unlock(&curr_mdesc_lock);
  239         
  240         return (0);
  241 
  242  done:
  243         if (buf != NULL)
  244                 (*mdesc_memops->mm_buf_free)(buf, mdesc_buf_size);
  245         return (rv);
  246 }
  247 
  248 md_t *
  249 md_get(void)
  250 {
  251         md_t *mdp;
  252         int rc;
  253         
  254         /*
  255          * XXX This should actually happen in init
  256          */
  257         if (curr_mdesc == NULL) {
  258                 if ((curr_mdesc = mdesc_alloc()) == NULL)
  259                         panic("machine description allocation failed");
  260                 if ((rc = mdesc_update()) != 0)
  261                         panic("machine description update failed: %d", rc);
  262         }
  263                 
  264         mtx_lock(&curr_mdesc_lock);
  265         curr_mdesc->md_refcount++;
  266         mdp = md_init_intern(curr_mdesc->md_addr, 
  267                              curr_mdesc->md_memops->mm_meta_alloc,
  268                              curr_mdesc->md_memops->mm_meta_free);
  269         mtx_unlock(&curr_mdesc_lock);
  270 
  271         return (mdp);
  272 }
  273 
  274 void
  275 md_put(md_t *ptr)
  276 {
  277         md_impl_t *mdp;
  278         
  279         mdp = (md_impl_t *)ptr;
  280 
  281 #ifdef notyet
  282         mtx_lock(&curr_mdesc_lock)
  283 
  284         /* XXX check against generation */
  285         if (curr_mdesc->md_gen == mdp->md_gen) {
  286                 curr_mdesc->md_refcount--;
  287                 mtx_unlock(&curr_mdesc_lock)
  288                 goto done;
  289         }
  290         mtx_unlock(&curr_mdesc_lock);
  291         /*
  292          * MD is on the out of date list 
  293          */
  294 
  295  done:
  296 #endif
  297         /*
  298          * We don't keep an out of date list currently
  299          */
  300         mtx_lock(&curr_mdesc_lock);
  301         curr_mdesc->md_refcount--;
  302         mtx_unlock(&curr_mdesc_lock);
  303         mdp->freep(mdp, sizeof(md_impl_t));
  304 }
  305 
  306 

Cache object: 072c5d925711b21c009fba80d51dcde4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.