1*29ed65e4Sriastradh /* $NetBSD: amdgpu_module.c,v 1.11 2022/07/23 12:52:09 riastradh Exp $ */
210f82c92Sriastradh
310f82c92Sriastradh /*-
410f82c92Sriastradh * Copyright (c) 2018 The NetBSD Foundation, Inc.
510f82c92Sriastradh * All rights reserved.
610f82c92Sriastradh *
710f82c92Sriastradh * This code is derived from software contributed to The NetBSD Foundation
810f82c92Sriastradh * by Taylor R. Campbell.
910f82c92Sriastradh *
1010f82c92Sriastradh * Redistribution and use in source and binary forms, with or without
1110f82c92Sriastradh * modification, are permitted provided that the following conditions
1210f82c92Sriastradh * are met:
1310f82c92Sriastradh * 1. Redistributions of source code must retain the above copyright
1410f82c92Sriastradh * notice, this list of conditions and the following disclaimer.
1510f82c92Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
1610f82c92Sriastradh * notice, this list of conditions and the following disclaimer in the
1710f82c92Sriastradh * documentation and/or other materials provided with the distribution.
1810f82c92Sriastradh *
1910f82c92Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2010f82c92Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2110f82c92Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2210f82c92Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2310f82c92Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2410f82c92Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2510f82c92Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2610f82c92Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2710f82c92Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2810f82c92Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2910f82c92Sriastradh * POSSIBILITY OF SUCH DAMAGE.
3010f82c92Sriastradh */
3110f82c92Sriastradh
3210f82c92Sriastradh #include <sys/cdefs.h>
33*29ed65e4Sriastradh __KERNEL_RCSID(0, "$NetBSD: amdgpu_module.c,v 1.11 2022/07/23 12:52:09 riastradh Exp $");
3410f82c92Sriastradh
3510f82c92Sriastradh #include <sys/types.h>
3610f82c92Sriastradh #include <sys/module.h>
3710f82c92Sriastradh #ifndef _MODULE
3810f82c92Sriastradh #include <sys/once.h>
3910f82c92Sriastradh #endif
4010f82c92Sriastradh #include <sys/systm.h>
4110f82c92Sriastradh
42f21b21b0Sriastradh #include <drm/drm_device.h>
4313222386Sriastradh #include <drm/drm_drv.h>
4410f82c92Sriastradh #include <drm/drm_sysctl.h>
4510f82c92Sriastradh
460caae222Sriastradh #include <linux/idr.h>
4713222386Sriastradh #include <linux/mutex.h>
4813222386Sriastradh
4913222386Sriastradh #include "amdgpu.h"
5010f82c92Sriastradh #include "amdgpu_amdkfd.h"
5110f82c92Sriastradh #include "amdgpu_drv.h"
5210f82c92Sriastradh
53*29ed65e4Sriastradh MODULE(MODULE_CLASS_DRIVER, amdgpu, "drmkms,drmkms_pci,drmkms_sched,drmkms_ttm"); /* XXX drmkms_i2c */
5410f82c92Sriastradh
5510f82c92Sriastradh #ifdef _MODULE
5610f82c92Sriastradh #include "ioconf.c"
5710f82c92Sriastradh #endif
5810f82c92Sriastradh
5910f82c92Sriastradh /* XXX Kludge to get these from amdgpu_drv.c. */
6010f82c92Sriastradh extern struct drm_driver *const amdgpu_drm_driver;
6113222386Sriastradh extern struct amdgpu_mgpu_info mgpu_info;
6210f82c92Sriastradh
630caae222Sriastradh /* XXX Kludge to replace DEFINE_IDA in amdgpu_ids.c. */
640caae222Sriastradh extern struct ida amdgpu_pasid_ida;
650caae222Sriastradh
6610f82c92Sriastradh struct drm_sysctl_def amdgpu_def = DRM_SYSCTL_INIT();
6710f82c92Sriastradh
6810f82c92Sriastradh static int
amdgpu_init(void)6910f82c92Sriastradh amdgpu_init(void)
7010f82c92Sriastradh {
7110f82c92Sriastradh int error;
7210f82c92Sriastradh
7310f82c92Sriastradh error = drm_guarantee_initialized();
7410f82c92Sriastradh if (error)
7510f82c92Sriastradh return error;
7610f82c92Sriastradh
7710f82c92Sriastradh amdgpu_drm_driver->num_ioctls = amdgpu_max_kms_ioctl;
7896277bfbSriastradh amdgpu_drm_driver->driver_features &= ~DRIVER_ATOMIC;
7910f82c92Sriastradh
8013222386Sriastradh linux_mutex_init(&mgpu_info.mutex);
810caae222Sriastradh ida_init(&amdgpu_pasid_ida);
8213222386Sriastradh
83058892faSriastradh error = amdgpu_sync_init();
84058892faSriastradh KASSERT(error == 0);
85058892faSriastradh error = amdgpu_fence_slab_init();
86058892faSriastradh KASSERT(error == 0);
87058892faSriastradh
8810f82c92Sriastradh #if notyet /* XXX amdgpu acpi */
8910f82c92Sriastradh amdgpu_register_atpx_handler();
9010f82c92Sriastradh #endif
9110f82c92Sriastradh
9210f82c92Sriastradh amdgpu_amdkfd_init();
9310f82c92Sriastradh drm_sysctl_init(&amdgpu_def);
9410f82c92Sriastradh
9510f82c92Sriastradh return 0;
9610f82c92Sriastradh }
9710f82c92Sriastradh
9810f82c92Sriastradh int amdgpu_guarantee_initialized(void); /* XXX */
9910f82c92Sriastradh int
amdgpu_guarantee_initialized(void)10010f82c92Sriastradh amdgpu_guarantee_initialized(void)
10110f82c92Sriastradh {
10210f82c92Sriastradh #ifdef _MODULE
10310f82c92Sriastradh return 0;
10410f82c92Sriastradh #else
10510f82c92Sriastradh static ONCE_DECL(amdgpu_init_once);
10610f82c92Sriastradh
10710f82c92Sriastradh return RUN_ONCE(&amdgpu_init_once, &amdgpu_init);
10810f82c92Sriastradh #endif
10910f82c92Sriastradh }
11010f82c92Sriastradh
11110f82c92Sriastradh static void
amdgpu_fini(void)11210f82c92Sriastradh amdgpu_fini(void)
11310f82c92Sriastradh {
11410f82c92Sriastradh
11510f82c92Sriastradh drm_sysctl_fini(&amdgpu_def);
11610f82c92Sriastradh amdgpu_amdkfd_fini();
11710f82c92Sriastradh #if notyet /* XXX amdgpu acpi */
11810f82c92Sriastradh amdgpu_unregister_atpx_handler();
11910f82c92Sriastradh #endif
120058892faSriastradh amdgpu_fence_slab_fini();
121058892faSriastradh amdgpu_sync_fini();
12213222386Sriastradh
1230caae222Sriastradh ida_destroy(&amdgpu_pasid_ida);
12413222386Sriastradh linux_mutex_destroy(&mgpu_info.mutex);
12510f82c92Sriastradh }
12610f82c92Sriastradh
12710f82c92Sriastradh static int
amdgpu_modcmd(modcmd_t cmd,void * arg __unused)12810f82c92Sriastradh amdgpu_modcmd(modcmd_t cmd, void *arg __unused)
12910f82c92Sriastradh {
13010f82c92Sriastradh int error;
13110f82c92Sriastradh
13210f82c92Sriastradh switch (cmd) {
13310f82c92Sriastradh case MODULE_CMD_INIT:
13410f82c92Sriastradh /* XXX Kludge it up... Must happen before attachment. */
13510f82c92Sriastradh #ifdef _MODULE
13610f82c92Sriastradh error = amdgpu_init();
13710f82c92Sriastradh #else
13810f82c92Sriastradh error = amdgpu_guarantee_initialized();
13910f82c92Sriastradh #endif
14010f82c92Sriastradh if (error) {
14110f82c92Sriastradh aprint_error("amdgpu: failed to initialize: %d\n",
14210f82c92Sriastradh error);
14310f82c92Sriastradh return error;
14410f82c92Sriastradh }
14510f82c92Sriastradh #ifdef _MODULE
14610f82c92Sriastradh error = config_init_component(cfdriver_ioconf_amdgpu,
14710f82c92Sriastradh cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
14810f82c92Sriastradh if (error) {
14910f82c92Sriastradh aprint_error("amdgpu: failed to init component"
15010f82c92Sriastradh ": %d\n", error);
15110f82c92Sriastradh amdgpu_fini();
15210f82c92Sriastradh return error;
15310f82c92Sriastradh }
15410f82c92Sriastradh #endif
15510f82c92Sriastradh return 0;
15610f82c92Sriastradh
15710f82c92Sriastradh case MODULE_CMD_FINI:
15810f82c92Sriastradh #ifdef _MODULE
15910f82c92Sriastradh error = config_fini_component(cfdriver_ioconf_amdgpu,
16010f82c92Sriastradh cfattach_ioconf_amdgpu, cfdata_ioconf_amdgpu);
16110f82c92Sriastradh if (error) {
16210f82c92Sriastradh aprint_error("amdgpu: failed to fini component"
16310f82c92Sriastradh ": %d\n", error);
16410f82c92Sriastradh return error;
16510f82c92Sriastradh }
16610f82c92Sriastradh #endif
16710f82c92Sriastradh amdgpu_fini();
16810f82c92Sriastradh return 0;
16910f82c92Sriastradh
17010f82c92Sriastradh default:
17110f82c92Sriastradh return ENOTTY;
17210f82c92Sriastradh }
17310f82c92Sriastradh }
174