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 };
47 
48 int sched_policy = KFD_SCHED_POLICY_HWS;
49 module_param(sched_policy, int, 0444);
50 MODULE_PARM_DESC(sched_policy,
51 	"Scheduling policy (0 = HWS (Default), 1 = HWS without over-subscription, 2 = Non-HWS (Used for debugging only)");
52 
53 int max_num_of_queues_per_device = KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT;
54 module_param(max_num_of_queues_per_device, int, 0444);
55 MODULE_PARM_DESC(max_num_of_queues_per_device,
56 	"Maximum number of supported queues per device (1 = Minimum, 4096 = default)");
57 
58 int send_sigterm;
59 module_param(send_sigterm, int, 0444);
60 MODULE_PARM_DESC(send_sigterm,
61 	"Send sigterm to HSA process on unhandled exception (0 = disable, 1 = enable)");
62 
63 static int amdkfd_init_completed;
64 
65 int kgd2kfd_init(unsigned int interface_version,
66 		const struct kgd2kfd_calls **g2f)
67 {
68 	if (!amdkfd_init_completed)
69 		return -EPROBE_DEFER;
70 
71 	/*
72 	 * Only one interface version is supported,
73 	 * no kfd/kgd version skew allowed.
74 	 */
75 	if (interface_version != KFD_INTERFACE_VERSION)
76 		return -EINVAL;
77 
78 	*g2f = &kgd2kfd;
79 
80 	return 0;
81 }
82 EXPORT_SYMBOL(kgd2kfd_init);
83 
84 void kgd2kfd_exit(void)
85 {
86 }
87 
88 static int __init kfd_module_init(void)
89 {
90 	int err;
91 
92 	/* Verify module parameters */
93 	if ((sched_policy < KFD_SCHED_POLICY_HWS) ||
94 		(sched_policy > KFD_SCHED_POLICY_NO_HWS)) {
95 		pr_err("sched_policy has invalid value\n");
96 		return -1;
97 	}
98 
99 	/* Verify module parameters */
100 	if ((max_num_of_queues_per_device < 1) ||
101 		(max_num_of_queues_per_device >
102 			KFD_MAX_NUM_OF_QUEUES_PER_DEVICE)) {
103 		pr_err("max_num_of_queues_per_device must be between 1 to KFD_MAX_NUM_OF_QUEUES_PER_DEVICE\n");
104 		return -1;
105 	}
106 
107 	err = kfd_chardev_init();
108 	if (err < 0)
109 		goto err_ioctl;
110 
111 	err = kfd_topology_init();
112 	if (err < 0)
113 		goto err_topology;
114 
115 	kfd_process_create_wq();
116 
117 	amdkfd_init_completed = 1;
118 
119 	dev_info(kfd_device, "Initialized module\n");
120 
121 	return 0;
122 
123 err_topology:
124 	kfd_chardev_exit();
125 err_ioctl:
126 	return err;
127 }
128 
129 static void __exit kfd_module_exit(void)
130 {
131 	amdkfd_init_completed = 0;
132 
133 	kfd_process_destroy_wq();
134 	kfd_topology_shutdown();
135 	kfd_chardev_exit();
136 	pr_info("amdkfd: Removed module\n");
137 }
138 
139 module_init(kfd_module_init);
140 module_exit(kfd_module_exit);
141 
142 MODULE_AUTHOR(KFD_DRIVER_AUTHOR);
143 MODULE_DESCRIPTION(KFD_DRIVER_DESC);
144 MODULE_LICENSE("GPL and additional rights");
145 MODULE_VERSION(__stringify(KFD_DRIVER_MAJOR) "."
146 	       __stringify(KFD_DRIVER_MINOR) "."
147 	       __stringify(KFD_DRIVER_PATCHLEVEL));
148