Home | History | Annotate | Line # | Download | only in nouveau
nouveau_module.c revision 1.3
      1 /*	$NetBSD: nouveau_module.c,v 1.3 2014/11/12 03:14:00 christos 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: nouveau_module.c,v 1.3 2014/11/12 03:14:00 christos Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/module.h>
     37 #ifndef _MODULE
     38 #include <sys/once.h>
     39 #endif
     40 #include <sys/systm.h>
     41 
     42 #include <drm/drmP.h>
     43 #include <drm/drm_sysctl.h>
     44 
     45 #include <core/object.h>
     46 #include <engine/device.h>
     47 
     48 MODULE(MODULE_CLASS_DRIVER, nouveau, "drmkms,drmkms_pci"); /* XXX drmkms_i2c, drmkms_ttm */
     49 
     50 #ifdef _MODULE
     51 #include "ioconf.c"
     52 #endif
     53 
     54 struct drm_sysctl_def nouveau_def = DRM_SYSCTL_INIT();
     55 
     56 extern struct drm_driver *const nouveau_drm_driver; /* XXX */
     57 
     58 static int
     59 nouveau_init(void)
     60 {
     61 	extern int drm_guarantee_initialized(void);
     62 	int error;
     63 
     64 	error = drm_guarantee_initialized();
     65 	if (error)
     66 		return error;
     67 
     68 	error = drm_pci_init(nouveau_drm_driver, NULL);
     69 	if (error) {
     70 		aprint_error("nouveau: failed to init pci: %d\n", error);
     71 		return error;
     72 	}
     73 
     74 	nouveau_objects_init();
     75 	nouveau_devices_init();
     76 #if 0				/* XXX nouveau acpi */
     77 	nouveau_register_dsm_handler();
     78 #endif
     79 	drm_sysctl_init(&nouveau_def);
     80 
     81 	return 0;
     82 }
     83 
     84 int	nouveau_guarantee_initialized(void); /* XXX */
     85 int
     86 nouveau_guarantee_initialized(void)
     87 {
     88 #ifdef _MODULE
     89 	return 0;
     90 #else
     91 	static ONCE_DECL(nouveau_init_once);
     92 
     93 	return RUN_ONCE(&nouveau_init_once, &nouveau_init);
     94 #endif
     95 }
     96 
     97 static void
     98 nouveau_fini(void)
     99 {
    100 
    101 	drm_sysctl_fini(&nouveau_def);
    102 #if 0				/* XXX nouveau acpi */
    103 	nouveau_unregister_dsm_handler();
    104 #endif
    105 	nouveau_devices_fini();
    106 	nouveau_objects_fini();
    107 	drm_pci_exit(nouveau_drm_driver, NULL);
    108 }
    109 
    110 static int
    111 nouveau_modcmd(modcmd_t cmd, void *arg __unused)
    112 {
    113 	int error;
    114 
    115 	switch (cmd) {
    116 	case MODULE_CMD_INIT:
    117 #ifdef _MODULE
    118 		error = nouveau_init();
    119 #else
    120 		error = nouveau_guarantee_initialized();
    121 #endif
    122 		if (error) {
    123 			aprint_error("nouveau: failed to initialize: %d\n",
    124 			    error);
    125 			return error;
    126 		}
    127 #ifdef _MODULE
    128 		error = config_init_component(cfdriver_ioconf_nouveau,
    129 		    cfattach_ioconf_nouveau, cfdata_ioconf_nouveau);
    130 		if (error) {
    131 			aprint_error("nouveau: failed to init component"
    132 			    ": %d\n", error);
    133 			nouveau_fini();
    134 			return error;
    135 		}
    136 #endif
    137 		return 0;
    138 
    139 	case MODULE_CMD_FINI:
    140 #ifdef _MODULE
    141 		error = config_fini_component(cfdriver_ioconf_nouveau,
    142 		    cfattach_ioconf_nouveau, cfdata_ioconf_nouveau);
    143 		if (error) {
    144 			aprint_error("nouveau: failed to fini component"
    145 			    ": %d\n", error);
    146 			return error;
    147 		}
    148 #endif
    149 		nouveau_fini();
    150 		return 0;
    151 
    152 	default:
    153 		return ENOTTY;
    154 	}
    155 }
    156