2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2013 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Host driver.
21 #include <linux/delay.h>
22 #include <linux/firmware.h>
23 #include <linux/pci.h>
25 #include <linux/mic_common.h>
26 #include <linux/mic_bus.h>
27 #include "../common/mic_dev.h"
28 #include "mic_device.h"
30 #include "mic_virtio.h"
32 static inline struct mic_device *mbdev_to_mdev(struct mbus_device *mbdev)
34 return dev_get_drvdata(mbdev->dev.parent);
38 mic_dma_map_page(struct device *dev, struct page *page,
39 unsigned long offset, size_t size, enum dma_data_direction dir,
40 struct dma_attrs *attrs)
42 void *va = phys_to_virt(page_to_phys(page)) + offset;
43 struct mic_device *mdev = dev_get_drvdata(dev->parent);
45 return mic_map_single(mdev, va, size);
49 mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
50 size_t size, enum dma_data_direction dir,
51 struct dma_attrs *attrs)
53 struct mic_device *mdev = dev_get_drvdata(dev->parent);
54 mic_unmap_single(mdev, dma_addr, size);
57 static struct dma_map_ops mic_dma_ops = {
58 .map_page = mic_dma_map_page,
59 .unmap_page = mic_dma_unmap_page,
62 static struct mic_irq *
63 _mic_request_threaded_irq(struct mbus_device *mbdev,
64 irq_handler_t handler, irq_handler_t thread_fn,
65 const char *name, void *data, int intr_src)
67 return mic_request_threaded_irq(mbdev_to_mdev(mbdev), handler,
68 thread_fn, name, data,
69 intr_src, MIC_INTR_DMA);
72 static void _mic_free_irq(struct mbus_device *mbdev,
73 struct mic_irq *cookie, void *data)
75 return mic_free_irq(mbdev_to_mdev(mbdev), cookie, data);
78 static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
80 struct mic_device *mdev = mbdev_to_mdev(mbdev);
81 mdev->ops->intr_workarounds(mdev);
84 static struct mbus_hw_ops mbus_hw_ops = {
85 .request_threaded_irq = _mic_request_threaded_irq,
86 .free_irq = _mic_free_irq,
87 .ack_interrupt = _mic_ack_interrupt,
91 * mic_reset - Reset the MIC device.
92 * @mdev: pointer to mic_device instance
94 static void mic_reset(struct mic_device *mdev)
98 #define MIC_RESET_TO (45)
100 reinit_completion(&mdev->reset_wait);
101 mdev->ops->reset_fw_ready(mdev);
102 mdev->ops->reset(mdev);
104 for (i = 0; i < MIC_RESET_TO; i++) {
105 if (mdev->ops->is_fw_ready(mdev))
108 * Resets typically take 10s of seconds to complete.
109 * Since an MMIO read is required to check if the
110 * firmware is ready or not, a 1 second delay works nicely.
114 mic_set_state(mdev, MIC_RESET_FAILED);
116 complete_all(&mdev->reset_wait);
119 /* Initialize the MIC bootparams */
120 void mic_bootparam_init(struct mic_device *mdev)
122 struct mic_bootparam *bootparam = mdev->dp;
124 bootparam->magic = cpu_to_le32(MIC_MAGIC);
125 bootparam->c2h_shutdown_db = mdev->shutdown_db;
126 bootparam->h2c_shutdown_db = -1;
127 bootparam->h2c_config_db = -1;
128 bootparam->shutdown_status = 0;
129 bootparam->shutdown_card = 0;
133 * mic_start - Start the MIC.
134 * @mdev: pointer to mic_device instance
135 * @buf: buffer containing boot string including firmware/ramdisk path.
137 * This function prepares an MIC for boot and initiates boot.
138 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
140 int mic_start(struct mic_device *mdev, const char *buf)
143 mutex_lock(&mdev->mic_mutex);
145 if (MIC_OFFLINE != mdev->state) {
149 if (!mdev->ops->is_fw_ready(mdev)) {
152 * The state will either be MIC_OFFLINE if the reset succeeded
153 * or MIC_RESET_FAILED if the firmware reset failed.
157 mdev->dma_mbdev = mbus_register_device(mdev->sdev->parent,
158 MBUS_DEV_DMA_HOST, &mic_dma_ops,
159 &mbus_hw_ops, mdev->mmio.va);
160 if (IS_ERR(mdev->dma_mbdev)) {
161 rc = PTR_ERR(mdev->dma_mbdev);
164 mdev->dma_ch = mic_request_dma_chan(mdev);
169 rc = mdev->ops->load_mic_fw(mdev, buf);
172 mic_smpt_restore(mdev);
173 mic_intr_restore(mdev);
174 mdev->intr_ops->enable_interrupts(mdev);
175 mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
176 mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
177 mdev->ops->send_firmware_intr(mdev);
178 mic_set_state(mdev, MIC_ONLINE);
181 dma_release_channel(mdev->dma_ch);
183 mbus_unregister_device(mdev->dma_mbdev);
185 mutex_unlock(&mdev->mic_mutex);
190 * mic_stop - Prepare the MIC for reset and trigger reset.
191 * @mdev: pointer to mic_device instance
192 * @force: force a MIC to reset even if it is already offline.
196 void mic_stop(struct mic_device *mdev, bool force)
198 mutex_lock(&mdev->mic_mutex);
199 if (MIC_OFFLINE != mdev->state || force) {
200 mic_virtio_reset_devices(mdev);
202 dma_release_channel(mdev->dma_ch);
205 mbus_unregister_device(mdev->dma_mbdev);
206 mic_bootparam_init(mdev);
208 if (MIC_RESET_FAILED == mdev->state)
210 mic_set_shutdown_status(mdev, MIC_NOP);
211 if (MIC_SUSPENDED != mdev->state)
212 mic_set_state(mdev, MIC_OFFLINE);
215 mutex_unlock(&mdev->mic_mutex);
219 * mic_shutdown - Initiate MIC shutdown.
220 * @mdev: pointer to mic_device instance
224 void mic_shutdown(struct mic_device *mdev)
226 struct mic_bootparam *bootparam = mdev->dp;
227 s8 db = bootparam->h2c_shutdown_db;
229 mutex_lock(&mdev->mic_mutex);
230 if (MIC_ONLINE == mdev->state && db != -1) {
231 bootparam->shutdown_card = 1;
232 mdev->ops->send_intr(mdev, db);
233 mic_set_state(mdev, MIC_SHUTTING_DOWN);
235 mutex_unlock(&mdev->mic_mutex);
239 * mic_shutdown_work - Handle shutdown interrupt from MIC.
240 * @work: The work structure.
242 * This work is scheduled whenever the host has received a shutdown
243 * interrupt from the MIC.
245 void mic_shutdown_work(struct work_struct *work)
247 struct mic_device *mdev = container_of(work, struct mic_device,
249 struct mic_bootparam *bootparam = mdev->dp;
251 mutex_lock(&mdev->mic_mutex);
252 mic_set_shutdown_status(mdev, bootparam->shutdown_status);
253 bootparam->shutdown_status = 0;
256 * if state is MIC_SUSPENDED, OSPM suspend is in progress. We do not
257 * change the state here so as to prevent users from booting the card
258 * during and after the suspend operation.
260 if (MIC_SHUTTING_DOWN != mdev->state &&
261 MIC_SUSPENDED != mdev->state)
262 mic_set_state(mdev, MIC_SHUTTING_DOWN);
263 mutex_unlock(&mdev->mic_mutex);
267 * mic_reset_trigger_work - Trigger MIC reset.
268 * @work: The work structure.
270 * This work is scheduled whenever the host wants to reset the MIC.
272 void mic_reset_trigger_work(struct work_struct *work)
274 struct mic_device *mdev = container_of(work, struct mic_device,
277 mic_stop(mdev, false);
281 * mic_complete_resume - Complete MIC Resume after an OSPM suspend/hibernate
283 * @mdev: pointer to mic_device instance
287 void mic_complete_resume(struct mic_device *mdev)
289 if (mdev->state != MIC_SUSPENDED) {
290 dev_warn(mdev->sdev->parent, "state %d should be %d\n",
291 mdev->state, MIC_SUSPENDED);
295 /* Make sure firmware is ready */
296 if (!mdev->ops->is_fw_ready(mdev))
297 mic_stop(mdev, true);
299 mutex_lock(&mdev->mic_mutex);
300 mic_set_state(mdev, MIC_OFFLINE);
301 mutex_unlock(&mdev->mic_mutex);
305 * mic_prepare_suspend - Handle suspend notification for the MIC device.
306 * @mdev: pointer to mic_device instance
310 void mic_prepare_suspend(struct mic_device *mdev)
312 unsigned long timeout;
314 #define MIC_SUSPEND_TIMEOUT (60 * HZ)
316 mutex_lock(&mdev->mic_mutex);
317 switch (mdev->state) {
320 * Card is already offline. Set state to MIC_SUSPENDED
321 * to prevent users from booting the card.
323 mic_set_state(mdev, MIC_SUSPENDED);
324 mutex_unlock(&mdev->mic_mutex);
328 * Card is online. Set state to MIC_SUSPENDING and notify
329 * MIC user space daemon which will issue card
330 * shutdown and reset.
332 mic_set_state(mdev, MIC_SUSPENDING);
333 mutex_unlock(&mdev->mic_mutex);
334 timeout = wait_for_completion_timeout(&mdev->reset_wait,
335 MIC_SUSPEND_TIMEOUT);
336 /* Force reset the card if the shutdown completion timed out */
338 mutex_lock(&mdev->mic_mutex);
339 mic_set_state(mdev, MIC_SUSPENDED);
340 mutex_unlock(&mdev->mic_mutex);
341 mic_stop(mdev, true);
344 case MIC_SHUTTING_DOWN:
346 * Card is shutting down. Set state to MIC_SUSPENDED
347 * to prevent further boot of the card.
349 mic_set_state(mdev, MIC_SUSPENDED);
350 mutex_unlock(&mdev->mic_mutex);
351 timeout = wait_for_completion_timeout(&mdev->reset_wait,
352 MIC_SUSPEND_TIMEOUT);
353 /* Force reset the card if the shutdown completion timed out */
355 mic_stop(mdev, true);
358 mutex_unlock(&mdev->mic_mutex);
364 * mic_suspend - Initiate MIC suspend. Suspend merely issues card shutdown.
365 * @mdev: pointer to mic_device instance
369 void mic_suspend(struct mic_device *mdev)
371 struct mic_bootparam *bootparam = mdev->dp;
372 s8 db = bootparam->h2c_shutdown_db;
374 mutex_lock(&mdev->mic_mutex);
375 if (MIC_SUSPENDING == mdev->state && db != -1) {
376 bootparam->shutdown_card = 1;
377 mdev->ops->send_intr(mdev, db);
378 mic_set_state(mdev, MIC_SUSPENDED);
380 mutex_unlock(&mdev->mic_mutex);