Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_module.c revision 1.8
      1 /*	$NetBSD: amdgpu_module.c,v 1.8 2021/12/19 12:30:14 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2018 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: amdgpu_module.c,v 1.8 2021/12/19 12:30:14 riastradh 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/drm_device.h>
     43 #include <drm/drm_drv.h>
     44 #include <drm/drm_sysctl.h>
     45 
     46 #include <linux/idr.h>
     47 #include <linux/mutex.h>
     48 
     49 #include "amdgpu.h"
     50 #include "amdgpu_amdkfd.h"
     51 #include "amdgpu_drv.h"
     52 
     53 MODULE(MODULE_CLASS_DRIVER, amdgpu, "drmkms,drmkms_pci"); /* XXX drmkms_i2c, drmkms_ttm */
     54 
     55 #ifdef _MODULE
     56 #include "ioconf.c"
     57 #endif
     58 
     59 /* XXX Kludge to get these from amdgpu_drv.c.  */
     60 extern struct drm_driver *const amdgpu_drm_driver;
     61 extern struct amdgpu_mgpu_info mgpu_info;
     62 
     63 /* XXX Kludge to replace DEFINE_IDA in amdgpu_ids.c.  */
     64 extern struct ida amdgpu_pasid_ida;
     65 
     66 struct drm_sysctl_def amdgpu_def = DRM_SYSCTL_INIT();
     67 
     68 static int
     69 amdgpu_init(void)
     70 {
     71 	int error;
     72 
     73 	error = drm_guarantee_initialized();
     74 	if (error)
     75 		return error;
     76 
     77 	amdgpu_drm_driver->num_ioctls = amdgpu_max_kms_ioctl;
     78 	amdgpu_drm_driver->driver_features &= ~DRIVER_ATOMIC;
     79 
     80 	linux_mutex_init(&mgpu_info.mutex);
     81 	ida_init(&amdgpu_pasid_ida);
     82 
     83 #if notyet			/* XXX amdgpu acpi */
     84 	amdgpu_register_atpx_handler();
     85 #endif
     86 
     87 	amdgpu_amdkfd_init();
     88 	drm_sysctl_init(&amdgpu_def);
     89 
     90 	return 0;
     91 }
     92 
     93 int	amdgpu_guarantee_initialized(void); /* XXX */
     94 int
     95 amdgpu_guarantee_initialized(void)
     96 {
     97 #ifdef _MODULE
     98 	return 0;
     99 #else
    100 	static ONCE_DECL(amdgpu_init_once);
    101 
    102 	return RUN_ONCE(&amdgpu_init_once, &amdgpu_init);
    103 #endif
    104 }
    105 
    106 static void
    107 amdgpu_fini(void)
    108 {
    109 
    110 	drm_sysctl_fini(&amdgpu_def);
    111 	amdgpu_amdkfd_fini();
    112 #if notyet			/* XXX amdgpu acpi */
    113 	amdgpu_unregister_atpx_handler();
    114 #endif
    115 
    116 	ida_destroy(&amdgpu_pasid_ida);
    117 	linux_mutex_destroy(&mgpu_info.mutex);
    118 }
    119 
    120 static int
    121 amdgpu_modcmd(modcmd_t cmd, void *arg __unused)
    122 {
    123 	int error;
    124 
    125 	switch (cmd) {
    126 	case MODULE_CMD_INIT:
    127 		/* XXX Kludge it up...  Must happen before attachment.  */
    128 #ifdef _MODULE
    129 		error = amdgpu_init();
    130 #else
    131 		error = amdgpu_guarantee_initialized();
    132 #endif
    133 		if (error) {
    134 			aprint_error("amdgpu: failed to initialize: %d\n",
    135 			    error);
    136 			return error;
    137 		}
    138 #ifdef _MODULE
    139 		error = config_init_component(cfdriver_ioconf_amdgpu,
    140 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
    141 		if (error) {
    142 			aprint_error("amdgpu: failed to init component"
    143 			    ": %d\n", error);
    144 			amdgpu_fini();
    145 			return error;
    146 		}
    147 #endif
    148 		return 0;
    149 
    150 	case MODULE_CMD_FINI:
    151 #ifdef _MODULE
    152 		error = config_fini_component(cfdriver_ioconf_amdgpu,
    153 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
    154 		if (error) {
    155 			aprint_error("amdgpu: failed to fini component"
    156 			    ": %d\n", error);
    157 			return error;
    158 		}
    159 #endif
    160 		amdgpu_fini();
    161 		return 0;
    162 
    163 	default:
    164 		return ENOTTY;
    165 	}
    166 }
    167