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		"20150122"
33 #define KFD_DRIVER_MAJOR	0
34 #define KFD_DRIVER_MINOR	7
35 #define KFD_DRIVER_PATCHLEVEL	1
36 
37 static const struct kgd2kfd_calls kgd2kfd = {
38 	.exit		= kgd2kfd_exit,
39 	.probe		= kgd2kfd_probe,
40 	.device_init	= kgd2kfd_device_init,
41 	.device_exit	= kgd2kfd_device_exit,
42 	.interrupt	= kgd2kfd_interrupt,
43 	.suspend	= kgd2kfd_suspend,
44 	.resume		= kgd2kfd_resume,
45 };
46 
47 int sched_policy = KFD_SCHED_POLICY_HWS;
48 module_param(sched_policy, int, 0444);
49 MODULE_PARM_DESC(sched_policy,
50 	"Scheduling policy (0 = HWS (Default), 1 = HWS without over-subscription, 2 = Non-HWS (Used for debugging only)");
51 
52 int max_num_of_queues_per_device = KFD_MAX_NUM_OF_QUEUES_PER_DEVICE_DEFAULT;
53 module_param(max_num_of_queues_per_device, int, 0444);
54 MODULE_PARM_DESC(max_num_of_queues_per_device,
55 	"Maximum number of supported queues per device (1 = Minimum, 4096 = default)");
56 
57 bool kgd2kfd_init(unsigned interface_version, const struct kgd2kfd_calls **g2f)
58 {
59 	/*
60 	 * Only one interface version is supported,
61 	 * no kfd/kgd version skew allowed.
62 	 */
63 	if (interface_version != KFD_INTERFACE_VERSION)
64 		return false;
65 
66 	*g2f = &kgd2kfd;
67 
68 	return true;
69 }
70 EXPORT_SYMBOL(kgd2kfd_init);
71 
72 void kgd2kfd_exit(void)
73 {
74 }
75 
76 static int __init kfd_module_init(void)
77 {
78 	int err;
79 
80 	/* Verify module parameters */
81 	if ((sched_policy < KFD_SCHED_POLICY_HWS) ||
82 		(sched_policy > KFD_SCHED_POLICY_NO_HWS)) {
83 		pr_err("kfd: sched_policy has invalid value\n");
84 		return -1;
85 	}
86 
87 	/* Verify module parameters */
88 	if ((max_num_of_queues_per_device < 1) ||
89 		(max_num_of_queues_per_device >
90 			KFD_MAX_NUM_OF_QUEUES_PER_DEVICE)) {
91 		pr_err("kfd: max_num_of_queues_per_device must be between 1 to KFD_MAX_NUM_OF_QUEUES_PER_DEVICE\n");
92 		return -1;
93 	}
94 
95 	err = kfd_pasid_init();
96 	if (err < 0)
97 		goto err_pasid;
98 
99 	err = kfd_chardev_init();
100 	if (err < 0)
101 		goto err_ioctl;
102 
103 	err = kfd_topology_init();
104 	if (err < 0)
105 		goto err_topology;
106 
107 	kfd_process_create_wq();
108 
109 	dev_info(kfd_device, "Initialized module\n");
110 
111 	return 0;
112 
113 err_topology:
114 	kfd_chardev_exit();
115 err_ioctl:
116 	kfd_pasid_exit();
117 err_pasid:
118 	return err;
119 }
120 
121 static void __exit kfd_module_exit(void)
122 {
123 	kfd_process_destroy_wq();
124 	kfd_topology_shutdown();
125 	kfd_chardev_exit();
126 	kfd_pasid_exit();
127 	dev_info(kfd_device, "Removed module\n");
128 }
129 
130 module_init(kfd_module_init);
131 module_exit(kfd_module_exit);
132 
133 MODULE_AUTHOR(KFD_DRIVER_AUTHOR);
134 MODULE_DESCRIPTION(KFD_DRIVER_DESC);
135 MODULE_LICENSE("GPL and additional rights");
136 MODULE_VERSION(__stringify(KFD_DRIVER_MAJOR) "."
137 	       __stringify(KFD_DRIVER_MINOR) "."
138 	       __stringify(KFD_DRIVER_PATCHLEVEL));
139