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 "kfd_priv.h"
28 
29 #define KFD_DRIVER_AUTHOR	"AMD Inc. and others"
30 
31 #define KFD_DRIVER_DESC		"Standalone HSA driver for AMD's GPUs"
32 #define KFD_DRIVER_DATE		"20141113"
33 #define KFD_DRIVER_MAJOR	0
34 #define KFD_DRIVER_MINOR	7
35 #define KFD_DRIVER_PATCHLEVEL	0
36 
37 const struct kfd2kgd_calls *kfd2kgd;
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 };
47 
48 int sched_policy = KFD_SCHED_POLICY_HWS;
49 module_param(sched_policy, int, 0444);
50 MODULE_PARM_DESC(sched_policy,
51 	"Kernel cmdline parameter that defines the amdkfd scheduling policy");
52 
53 int max_num_of_processes = KFD_MAX_NUM_OF_PROCESSES_DEFAULT;
54 module_param(max_num_of_processes, int, 0444);
55 MODULE_PARM_DESC(max_num_of_processes,
56 	"Kernel cmdline parameter that defines the amdkfd maximum number of supported processes");
57 
58 int max_num_of_queues_per_process = KFD_MAX_NUM_OF_QUEUES_PER_PROCESS_DEFAULT;
59 module_param(max_num_of_queues_per_process, int, 0444);
60 MODULE_PARM_DESC(max_num_of_queues_per_process,
61 	"Kernel cmdline parameter that defines the amdkfd maximum number of supported queues per process");
62 
63 bool kgd2kfd_init(unsigned interface_version,
64 		  const struct kfd2kgd_calls *f2g,
65 		  const struct kgd2kfd_calls **g2f)
66 {
67 	/*
68 	 * Only one interface version is supported,
69 	 * no kfd/kgd version skew allowed.
70 	 */
71 	if (interface_version != KFD_INTERFACE_VERSION)
72 		return false;
73 
74 	/* Protection against multiple amd kgd loads */
75 	if (kfd2kgd)
76 		return true;
77 
78 	kfd2kgd = f2g;
79 	*g2f = &kgd2kfd;
80 
81 	return true;
82 }
83 EXPORT_SYMBOL(kgd2kfd_init);
84 
85 void kgd2kfd_exit(void)
86 {
87 }
88 
89 static int __init kfd_module_init(void)
90 {
91 	int err;
92 
93 	kfd2kgd = NULL;
94 
95 	/* Verify module parameters */
96 	if ((sched_policy < KFD_SCHED_POLICY_HWS) ||
97 		(sched_policy > KFD_SCHED_POLICY_NO_HWS)) {
98 		pr_err("kfd: sched_policy has invalid value\n");
99 		return -1;
100 	}
101 
102 	/* Verify module parameters */
103 	if ((max_num_of_processes < 0) ||
104 		(max_num_of_processes > KFD_MAX_NUM_OF_PROCESSES)) {
105 		pr_err("kfd: max_num_of_processes must be between 0 to KFD_MAX_NUM_OF_PROCESSES\n");
106 		return -1;
107 	}
108 
109 	if ((max_num_of_queues_per_process < 0) ||
110 		(max_num_of_queues_per_process >
111 			KFD_MAX_NUM_OF_QUEUES_PER_PROCESS)) {
112 		pr_err("kfd: max_num_of_queues_per_process must be between 0 to KFD_MAX_NUM_OF_QUEUES_PER_PROCESS\n");
113 		return -1;
114 	}
115 
116 	err = kfd_pasid_init();
117 	if (err < 0)
118 		goto err_pasid;
119 
120 	err = kfd_chardev_init();
121 	if (err < 0)
122 		goto err_ioctl;
123 
124 	err = kfd_topology_init();
125 	if (err < 0)
126 		goto err_topology;
127 
128 	kfd_process_create_wq();
129 
130 	dev_info(kfd_device, "Initialized module\n");
131 
132 	return 0;
133 
134 err_topology:
135 	kfd_chardev_exit();
136 err_ioctl:
137 	kfd_pasid_exit();
138 err_pasid:
139 	return err;
140 }
141 
142 static void __exit kfd_module_exit(void)
143 {
144 	kfd_process_destroy_wq();
145 	kfd_topology_shutdown();
146 	kfd_chardev_exit();
147 	kfd_pasid_exit();
148 	dev_info(kfd_device, "Removed module\n");
149 }
150 
151 module_init(kfd_module_init);
152 module_exit(kfd_module_exit);
153 
154 MODULE_AUTHOR(KFD_DRIVER_AUTHOR);
155 MODULE_DESCRIPTION(KFD_DRIVER_DESC);
156 MODULE_LICENSE("GPL and additional rights");
157 MODULE_VERSION(__stringify(KFD_DRIVER_MAJOR) "."
158 	       __stringify(KFD_DRIVER_MINOR) "."
159 	       __stringify(KFD_DRIVER_PATCHLEVEL));
160