Home | History | Annotate | Line # | Download | only in linux
linux_module.c revision 1.4
      1 /*	$NetBSD: linux_module.c,v 1.4 2014/07/16 20:59:58 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_module.c,v 1.4 2014/07/16 20:59:58 riastradh Exp $");
     34 
     35 #include <sys/module.h>
     36 #ifndef _MODULE
     37 #include <sys/once.h>
     38 #endif
     39 
     40 #include <linux/highmem.h>
     41 #include <linux/idr.h>
     42 #include <linux/io.h>
     43 #include <linux/mutex.h>
     44 #include <linux/reservation.h>
     45 #include <linux/workqueue.h>
     46 
     47 MODULE(MODULE_CLASS_MISC, drmkms_linux, NULL);
     48 
     49 DEFINE_WW_CLASS(reservation_ww_class __cacheline_aligned);
     50 
     51 static int
     52 linux_init(void)
     53 {
     54 	int error;
     55 
     56 	error = linux_idr_module_init();
     57 	if (error) {
     58 		printf("linux: unable to initialize idr: %d\n", error);
     59 		goto fail0;
     60 	}
     61 
     62 	error = linux_kmap_init();
     63 	if (error) {
     64 		printf("linux: unable to initialize kmap: %d\n", error);
     65 		goto fail1;
     66 	}
     67 
     68 	error = linux_workqueue_init();
     69 	if (error) {
     70 		printf("linux: unable to initialize workqueues: %d\n", error);
     71 		goto fail2;
     72 	}
     73 
     74 	error = linux_writecomb_init();
     75 	if (error) {
     76 		printf("linux: unable to initialize write-combining: %d\n",
     77 		    error);
     78 		goto fail3;
     79 	}
     80 
     81 	return 0;
     82 
     83 fail4: __unused
     84 	linux_writecomb_fini();
     85 fail3:	linux_workqueue_fini();
     86 fail2:	linux_kmap_fini();
     87 fail1:	linux_idr_module_fini();
     88 fail0:	return error;
     89 }
     90 
     91 int	linux_guarantee_initialized(void); /* XXX */
     92 int
     93 linux_guarantee_initialized(void)
     94 {
     95 #ifdef _MODULE
     96 	return 0;
     97 #else
     98 	static ONCE_DECL(linux_init_once);
     99 
    100 	return RUN_ONCE(&linux_init_once, &linux_init);
    101 #endif
    102 }
    103 
    104 static void
    105 linux_fini(void)
    106 {
    107 
    108 	linux_writecomb_fini();
    109 	linux_workqueue_fini();
    110 	linux_kmap_fini();
    111 }
    112 
    113 static int
    114 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
    115 {
    116 
    117 	switch (cmd) {
    118 	case MODULE_CMD_INIT:
    119 #ifdef _MODULE
    120 		return linux_init();
    121 #else
    122 		return linux_guarantee_initialized();
    123 #endif
    124 	case MODULE_CMD_FINI:
    125 		linux_fini();
    126 		return 0;
    127 	default:
    128 		return ENOTTY;
    129 	}
    130 }
    131