Home | History | Annotate | Line # | Download | only in amdgpu
      1 /*	$NetBSD: amdgpu_module.c,v 1.11 2022/07/23 12:52:09 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.11 2022/07/23 12:52:09 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,drmkms_sched,drmkms_ttm"); /* XXX drmkms_i2c */
     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 	error = amdgpu_sync_init();
     84 	KASSERT(error == 0);
     85 	error = amdgpu_fence_slab_init();
     86 	KASSERT(error == 0);
     87 
     88 #if notyet			/* XXX amdgpu acpi */
     89 	amdgpu_register_atpx_handler();
     90 #endif
     91 
     92 	amdgpu_amdkfd_init();
     93 	drm_sysctl_init(&amdgpu_def);
     94 
     95 	return 0;
     96 }
     97 
     98 int	amdgpu_guarantee_initialized(void); /* XXX */
     99 int
    100 amdgpu_guarantee_initialized(void)
    101 {
    102 #ifdef _MODULE
    103 	return 0;
    104 #else
    105 	static ONCE_DECL(amdgpu_init_once);
    106 
    107 	return RUN_ONCE(&amdgpu_init_once, &amdgpu_init);
    108 #endif
    109 }
    110 
    111 static void
    112 amdgpu_fini(void)
    113 {
    114 
    115 	drm_sysctl_fini(&amdgpu_def);
    116 	amdgpu_amdkfd_fini();
    117 #if notyet			/* XXX amdgpu acpi */
    118 	amdgpu_unregister_atpx_handler();
    119 #endif
    120 	amdgpu_fence_slab_fini();
    121 	amdgpu_sync_fini();
    122 
    123 	ida_destroy(&amdgpu_pasid_ida);
    124 	linux_mutex_destroy(&mgpu_info.mutex);
    125 }
    126 
    127 static int
    128 amdgpu_modcmd(modcmd_t cmd, void *arg __unused)
    129 {
    130 	int error;
    131 
    132 	switch (cmd) {
    133 	case MODULE_CMD_INIT:
    134 		/* XXX Kludge it up...  Must happen before attachment.  */
    135 #ifdef _MODULE
    136 		error = amdgpu_init();
    137 #else
    138 		error = amdgpu_guarantee_initialized();
    139 #endif
    140 		if (error) {
    141 			aprint_error("amdgpu: failed to initialize: %d\n",
    142 			    error);
    143 			return error;
    144 		}
    145 #ifdef _MODULE
    146 		error = config_init_component(cfdriver_ioconf_amdgpu,
    147 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
    148 		if (error) {
    149 			aprint_error("amdgpu: failed to init component"
    150 			    ": %d\n", error);
    151 			amdgpu_fini();
    152 			return error;
    153 		}
    154 #endif
    155 		return 0;
    156 
    157 	case MODULE_CMD_FINI:
    158 #ifdef _MODULE
    159 		error = config_fini_component(cfdriver_ioconf_amdgpu,
    160 		    cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
    161 		if (error) {
    162 			aprint_error("amdgpu: failed to fini component"
    163 			    ": %d\n", error);
    164 			return error;
    165 		}
    166 #endif
    167 		amdgpu_fini();
    168 		return 0;
    169 
    170 	default:
    171 		return ENOTTY;
    172 	}
    173 }
    174