1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2015 Intel Corporation.
7 * Intel MIC Coprocessor State Management (COSM) Driver
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/idr.h>
13 #include <linux/slab.h>
14 #include <linux/cred.h>
15 #include "cosm_main.h"
17 static const char cosm_driver_name
[] = "mic";
19 /* COSM ID allocator */
20 static struct ida g_cosm_ida
;
21 /* Class of MIC devices for sysfs accessibility. */
22 static struct class *g_cosm_class
;
23 /* Number of MIC devices */
24 static atomic_t g_num_dev
;
27 * cosm_hw_reset - Issue a HW reset for the MIC device
28 * @cdev: pointer to cosm_device instance
30 static void cosm_hw_reset(struct cosm_device
*cdev
, bool force
)
34 #define MIC_RESET_TO (45)
35 if (force
&& cdev
->hw_ops
->force_reset
)
36 cdev
->hw_ops
->force_reset(cdev
);
38 cdev
->hw_ops
->reset(cdev
);
40 for (i
= 0; i
< MIC_RESET_TO
; i
++) {
41 if (cdev
->hw_ops
->ready(cdev
)) {
42 cosm_set_state(cdev
, MIC_READY
);
46 * Resets typically take 10s of seconds to complete.
47 * Since an MMIO read is required to check if the
48 * firmware is ready or not, a 1 second delay works nicely.
52 cosm_set_state(cdev
, MIC_RESET_FAILED
);
56 * cosm_start - Start the MIC
57 * @cdev: pointer to cosm_device instance
59 * This function prepares an MIC for boot and initiates boot.
60 * RETURNS: An appropriate -ERRNO error value on error, or 0 for success.
62 int cosm_start(struct cosm_device
*cdev
)
64 const struct cred
*orig_cred
;
65 struct cred
*override_cred
;
68 mutex_lock(&cdev
->cosm_mutex
);
69 if (!cdev
->bootmode
) {
70 dev_err(&cdev
->dev
, "%s %d bootmode not set\n",
76 if (cdev
->state
!= MIC_READY
) {
77 dev_err(&cdev
->dev
, "%s %d MIC state not READY\n",
82 if (!cdev
->hw_ops
->ready(cdev
)) {
83 cosm_hw_reset(cdev
, false);
85 * The state will either be MIC_READY if the reset succeeded
86 * or MIC_RESET_FAILED if the firmware reset failed.
92 * Set credentials to root to allow non-root user to download initramsfs
93 * with 600 permissions
95 override_cred
= prepare_creds();
97 dev_err(&cdev
->dev
, "%s %d prepare_creds failed\n",
102 override_cred
->fsuid
= GLOBAL_ROOT_UID
;
103 orig_cred
= override_creds(override_cred
);
105 rc
= cdev
->hw_ops
->start(cdev
, cdev
->index
);
107 revert_creds(orig_cred
);
108 put_cred(override_cred
);
113 * If linux is being booted, card is treated 'online' only
114 * when the scif interface in the card is up. If anything else
115 * is booted, we set card to 'online' immediately.
117 if (!strcmp(cdev
->bootmode
, "linux"))
118 cosm_set_state(cdev
, MIC_BOOTING
);
120 cosm_set_state(cdev
, MIC_ONLINE
);
122 mutex_unlock(&cdev
->cosm_mutex
);
124 dev_err(&cdev
->dev
, "cosm_start failed rc %d\n", rc
);
129 * cosm_stop - Prepare the MIC for reset and trigger reset
130 * @cdev: pointer to cosm_device instance
131 * @force: force a MIC to reset even if it is already reset and ready.
135 void cosm_stop(struct cosm_device
*cdev
, bool force
)
137 mutex_lock(&cdev
->cosm_mutex
);
138 if (cdev
->state
!= MIC_READY
|| force
) {
140 * Don't call hw_ops if they have been called previously.
141 * stop(..) calls device_unregister and will crash the system if
142 * called multiple times.
144 u8 state
= cdev
->state
== MIC_RESETTING
?
145 cdev
->prev_state
: cdev
->state
;
146 bool call_hw_ops
= state
!= MIC_RESET_FAILED
&&
149 if (cdev
->state
!= MIC_RESETTING
)
150 cosm_set_state(cdev
, MIC_RESETTING
);
151 cdev
->heartbeat_watchdog_enable
= false;
153 cdev
->hw_ops
->stop(cdev
, force
);
154 cosm_hw_reset(cdev
, force
);
155 cosm_set_shutdown_status(cdev
, MIC_NOP
);
156 if (call_hw_ops
&& cdev
->hw_ops
->post_reset
)
157 cdev
->hw_ops
->post_reset(cdev
, cdev
->state
);
159 mutex_unlock(&cdev
->cosm_mutex
);
160 flush_work(&cdev
->scif_work
);
164 * cosm_reset_trigger_work - Trigger MIC reset
165 * @work: The work structure
167 * This work is scheduled whenever the host wants to reset the MIC.
169 static void cosm_reset_trigger_work(struct work_struct
*work
)
171 struct cosm_device
*cdev
= container_of(work
, struct cosm_device
,
173 cosm_stop(cdev
, false);
177 * cosm_reset - Schedule MIC reset
178 * @cdev: pointer to cosm_device instance
180 * RETURNS: An -EINVAL if the card is already READY or 0 for success.
182 int cosm_reset(struct cosm_device
*cdev
)
186 mutex_lock(&cdev
->cosm_mutex
);
187 if (cdev
->state
!= MIC_READY
) {
188 if (cdev
->state
!= MIC_RESETTING
) {
189 cdev
->prev_state
= cdev
->state
;
190 cosm_set_state(cdev
, MIC_RESETTING
);
191 schedule_work(&cdev
->reset_trigger_work
);
194 dev_err(&cdev
->dev
, "%s %d MIC is READY\n", __func__
, __LINE__
);
197 mutex_unlock(&cdev
->cosm_mutex
);
202 * cosm_shutdown - Initiate MIC shutdown.
203 * @cdev: pointer to cosm_device instance
207 int cosm_shutdown(struct cosm_device
*cdev
)
209 struct cosm_msg msg
= { .id
= COSM_MSG_SHUTDOWN
};
212 mutex_lock(&cdev
->cosm_mutex
);
213 if (cdev
->state
!= MIC_ONLINE
) {
215 dev_err(&cdev
->dev
, "%s %d skipping shutdown in state: %s\n",
216 __func__
, __LINE__
, cosm_state_string
[cdev
->state
]);
222 dev_err(&cdev
->dev
, "%s %d scif endpoint not connected rc %d\n",
223 __func__
, __LINE__
, rc
);
227 rc
= scif_send(cdev
->epd
, &msg
, sizeof(msg
), SCIF_SEND_BLOCK
);
229 dev_err(&cdev
->dev
, "%s %d scif_send failed rc %d\n",
230 __func__
, __LINE__
, rc
);
233 cdev
->heartbeat_watchdog_enable
= false;
234 cosm_set_state(cdev
, MIC_SHUTTING_DOWN
);
237 mutex_unlock(&cdev
->cosm_mutex
);
241 static int cosm_driver_probe(struct cosm_device
*cdev
)
245 /* Initialize SCIF server at first probe */
246 if (atomic_add_return(1, &g_num_dev
) == 1) {
247 rc
= cosm_scif_init();
251 mutex_init(&cdev
->cosm_mutex
);
252 INIT_WORK(&cdev
->reset_trigger_work
, cosm_reset_trigger_work
);
253 INIT_WORK(&cdev
->scif_work
, cosm_scif_work
);
254 cdev
->sysfs_heartbeat_enable
= true;
255 cosm_sysfs_init(cdev
);
256 cdev
->sdev
= device_create_with_groups(g_cosm_class
, cdev
->dev
.parent
,
257 MKDEV(0, cdev
->index
), cdev
, cdev
->attr_group
,
258 "mic%d", cdev
->index
);
259 if (IS_ERR(cdev
->sdev
)) {
260 rc
= PTR_ERR(cdev
->sdev
);
261 dev_err(&cdev
->dev
, "device_create_with_groups failed rc %d\n",
266 cdev
->state_sysfs
= sysfs_get_dirent(cdev
->sdev
->kobj
.sd
,
268 if (!cdev
->state_sysfs
) {
270 dev_err(&cdev
->dev
, "sysfs_get_dirent failed rc %d\n", rc
);
273 cosm_create_debug_dir(cdev
);
276 device_destroy(g_cosm_class
, MKDEV(0, cdev
->index
));
278 if (atomic_dec_and_test(&g_num_dev
))
283 static void cosm_driver_remove(struct cosm_device
*cdev
)
285 cosm_delete_debug_dir(cdev
);
286 sysfs_put(cdev
->state_sysfs
);
287 device_destroy(g_cosm_class
, MKDEV(0, cdev
->index
));
288 flush_work(&cdev
->reset_trigger_work
);
289 cosm_stop(cdev
, false);
290 if (atomic_dec_and_test(&g_num_dev
))
293 /* These sysfs entries might have allocated */
294 kfree(cdev
->cmdline
);
295 kfree(cdev
->firmware
);
296 kfree(cdev
->ramdisk
);
297 kfree(cdev
->bootmode
);
300 static int cosm_suspend(struct device
*dev
)
302 struct cosm_device
*cdev
= dev_to_cosm(dev
);
304 mutex_lock(&cdev
->cosm_mutex
);
305 switch (cdev
->state
) {
307 * Suspend/freeze hooks in userspace have already shutdown the card.
308 * Card should be 'ready' in most cases. It is however possible that
309 * some userspace application initiated a boot. In those cases, we
310 * simply reset the card.
314 case MIC_SHUTTING_DOWN
:
315 mutex_unlock(&cdev
->cosm_mutex
);
316 cosm_stop(cdev
, false);
319 mutex_unlock(&cdev
->cosm_mutex
);
325 static const struct dev_pm_ops cosm_pm_ops
= {
326 .suspend
= cosm_suspend
,
327 .freeze
= cosm_suspend
330 static struct cosm_driver cosm_driver
= {
332 .name
= KBUILD_MODNAME
,
333 .owner
= THIS_MODULE
,
336 .probe
= cosm_driver_probe
,
337 .remove
= cosm_driver_remove
340 static int __init
cosm_init(void)
346 g_cosm_class
= class_create(THIS_MODULE
, cosm_driver_name
);
347 if (IS_ERR(g_cosm_class
)) {
348 ret
= PTR_ERR(g_cosm_class
);
349 pr_err("class_create failed ret %d\n", ret
);
350 goto cleanup_debugfs
;
353 ida_init(&g_cosm_ida
);
354 ret
= cosm_register_driver(&cosm_driver
);
356 pr_err("cosm_register_driver failed ret %d\n", ret
);
361 ida_destroy(&g_cosm_ida
);
362 class_destroy(g_cosm_class
);
368 static void __exit
cosm_exit(void)
370 cosm_unregister_driver(&cosm_driver
);
371 ida_destroy(&g_cosm_ida
);
372 class_destroy(g_cosm_class
);
376 module_init(cosm_init
);
377 module_exit(cosm_exit
);
379 MODULE_AUTHOR("Intel Corporation");
380 MODULE_DESCRIPTION("Intel(R) MIC Coprocessor State Management (COSM) Driver");
381 MODULE_LICENSE("GPL v2");