1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/moduleparam.h>
26 #include <linux/device.h>
27 #include <linux/printk.h>
28 #include "kfd_priv.h"
29 
30 #define KFD_DRIVER_AUTHOR	"AMD Inc. and others"
31 
32 #define KFD_DRIVER_DESC		"Standalone HSA driver for AMD's GPUs"
33 #define KFD_DRIVER_DATE		"20150421"
34 #define KFD_DRIVER_MAJOR	0
35 #define KFD_DRIVER_MINOR	7
36 #define KFD_DRIVER_PATCHLEVEL	2
37 
38 static const struct kgd2kfd_calls kgd2kfd = {
39 	.exit		= kgd2kfd_exit,
40 	.probe		= kgd2kfd_probe,
41 	.device_init	= kgd2kfd_device_init,
42 	.device_exit	= kgd2kfd_device_exit,
43 	.interrupt	= kgd2kfd_interrupt,
44 	.suspend	= kgd2kfd_suspend,
45 	.resume		= kgd2kfd_resume,
46 	.quiesce_mm	= kgd2kfd_quiesce_mm,
47 	.resume_mm	= kgd2kfd_resume_mm,
48 	.schedule_evict_and_restore_process =
49 			  kgd2kfd_schedule_evict_and_restore_process,
50 };
51 
52 int sched_policy = KFD_SCHED_POLICY_HWS;
53 module_param(sched_policy, int, 0444);
54 MODULE_PARM_DESC(sched_policy,
55 	"Scheduling policy (0 = HWS (Default), 1 = HWS without over-subscription, 2 = Non-HWS (Used for debugging only)");
56 
57 int hws_max_conc_proc = 8;
58 module_param(hws_max_conc_proc, int, 0444);
59 MODULE_PARM_DESC(hws_max_conc_proc,
60 	"Max # processes HWS can execute concurrently when sched_policy=0 (0 = no concurrency, #VMIDs for KFD = Maximum(default))");
61 
62 int cwsr_enable = 1;
63 module_param(cwsr_enable, int, 0444);
64 MODULE_PARM_DESC(cwsr_enable, "CWSR enable (0 = Off, 1 = On (Default))");
65 
66 int max_num_of_queues_per_device = KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT;
67 module_param(max_num_of_queues_per_device, int, 0444);
68 MODULE_PARM_DESC(max_num_of_queues_per_device,
69 	"Maximum number of supported queues per device (1 = Minimum, 4096 = default)");
70 
71 int send_sigterm;
72 module_param(send_sigterm, int, 0444);
73 MODULE_PARM_DESC(send_sigterm,
74 	"Send sigterm to HSA process on unhandled exception (0 = disable, 1 = enable)");
75 
76 int debug_largebar;
77 module_param(debug_largebar, int, 0444);
78 MODULE_PARM_DESC(debug_largebar,
79 	"Debug large-bar flag used to simulate large-bar capability on non-large bar machine (0 = disable, 1 = enable)");
80 
81 int ignore_crat;
82 module_param(ignore_crat, int, 0444);
83 MODULE_PARM_DESC(ignore_crat,
84 	"Ignore CRAT table during KFD initialization (0 = use CRAT (default), 1 = ignore CRAT)");
85 
86 int vega10_noretry;
87 module_param_named(noretry, vega10_noretry, int, 0644);
88 MODULE_PARM_DESC(noretry,
89 	"Set sh_mem_config.retry_disable on Vega10 (0 = retry enabled (default), 1 = retry disabled)");
90 
91 static int amdkfd_init_completed;
92 
93 int kgd2kfd_init(unsigned int interface_version,
94 		const struct kgd2kfd_calls **g2f)
95 {
96 	if (!amdkfd_init_completed)
97 		return -EPROBE_DEFER;
98 
99 	/*
100 	 * Only one interface version is supported,
101 	 * no kfd/kgd version skew allowed.
102 	 */
103 	if (interface_version != KFD_INTERFACE_VERSION)
104 		return -EINVAL;
105 
106 	*g2f = &kgd2kfd;
107 
108 	return 0;
109 }
110 EXPORT_SYMBOL(kgd2kfd_init);
111 
112 void kgd2kfd_exit(void)
113 {
114 }
115 
116 static int __init kfd_module_init(void)
117 {
118 	int err;
119 
120 	/* Verify module parameters */
121 	if ((sched_policy < KFD_SCHED_POLICY_HWS) ||
122 		(sched_policy > KFD_SCHED_POLICY_NO_HWS)) {
123 		pr_err("sched_policy has invalid value\n");
124 		return -1;
125 	}
126 
127 	/* Verify module parameters */
128 	if ((max_num_of_queues_per_device < 1) ||
129 		(max_num_of_queues_per_device >
130 			KFD_MAX_NUM_OF_QUEUES_PER_DEVICE)) {
131 		pr_err("max_num_of_queues_per_device must be between 1 to KFD_MAX_NUM_OF_QUEUES_PER_DEVICE\n");
132 		return -1;
133 	}
134 
135 	err = kfd_chardev_init();
136 	if (err < 0)
137 		goto err_ioctl;
138 
139 	err = kfd_topology_init();
140 	if (err < 0)
141 		goto err_topology;
142 
143 	err = kfd_process_create_wq();
144 	if (err < 0)
145 		goto err_create_wq;
146 
147 	kfd_debugfs_init();
148 
149 	amdkfd_init_completed = 1;
150 
151 	dev_info(kfd_device, "Initialized module\n");
152 
153 	return 0;
154 
155 err_create_wq:
156 	kfd_topology_shutdown();
157 err_topology:
158 	kfd_chardev_exit();
159 err_ioctl:
160 	return err;
161 }
162 
163 static void __exit kfd_module_exit(void)
164 {
165 	amdkfd_init_completed = 0;
166 
167 	kfd_debugfs_fini();
168 	kfd_process_destroy_wq();
169 	kfd_topology_shutdown();
170 	kfd_chardev_exit();
171 	pr_info("amdkfd: Removed module\n");
172 }
173 
174 module_init(kfd_module_init);
175 module_exit(kfd_module_exit);
176 
177 MODULE_AUTHOR(KFD_DRIVER_AUTHOR);
178 MODULE_DESCRIPTION(KFD_DRIVER_DESC);
179 MODULE_LICENSE("GPL and additional rights");
180 MODULE_VERSION(__stringify(KFD_DRIVER_MAJOR) "."
181 	       __stringify(KFD_DRIVER_MINOR) "."
182 	       __stringify(KFD_DRIVER_PATCHLEVEL));
183