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>
24 #include <linux/kmod.h>
26 #include <linux/mic_common.h>
27 #include <linux/mic_bus.h>
28 #include "../common/mic_dev.h"
29 #include "mic_device.h"
31 #include "mic_virtio.h"
33 static inline struct mic_device
*scdev_to_mdev(struct scif_hw_dev
*scdev
)
35 return dev_get_drvdata(scdev
->dev
.parent
);
38 static void *__mic_dma_alloc(struct device
*dev
, size_t size
,
39 dma_addr_t
*dma_handle
, gfp_t gfp
,
40 struct dma_attrs
*attrs
)
42 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
43 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
45 void *va
= kmalloc(size
, gfp
);
48 tmp
= mic_map_single(mdev
, va
, size
);
49 if (dma_mapping_error(dev
, tmp
)) {
59 static void __mic_dma_free(struct device
*dev
, size_t size
, void *vaddr
,
60 dma_addr_t dma_handle
, struct dma_attrs
*attrs
)
62 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
63 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
65 mic_unmap_single(mdev
, dma_handle
, size
);
70 __mic_dma_map_page(struct device
*dev
, struct page
*page
, unsigned long offset
,
71 size_t size
, enum dma_data_direction dir
,
72 struct dma_attrs
*attrs
)
74 void *va
= phys_to_virt(page_to_phys(page
)) + offset
;
75 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
76 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
78 return mic_map_single(mdev
, va
, size
);
82 __mic_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
83 size_t size
, enum dma_data_direction dir
,
84 struct dma_attrs
*attrs
)
86 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
87 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
89 mic_unmap_single(mdev
, dma_addr
, size
);
92 static int __mic_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
93 int nents
, enum dma_data_direction dir
,
94 struct dma_attrs
*attrs
)
96 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
97 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
98 struct scatterlist
*s
;
102 ret
= dma_map_sg(mdev
->sdev
->parent
, sg
, nents
, dir
);
106 for_each_sg(sg
, s
, nents
, i
) {
107 da
= mic_map(mdev
, sg_dma_address(s
) + s
->offset
, s
->length
);
110 sg_dma_address(s
) = da
;
114 for_each_sg(sg
, s
, i
, j
) {
115 mic_unmap(mdev
, sg_dma_address(s
), s
->length
);
116 sg_dma_address(s
) = mic_to_dma_addr(mdev
, sg_dma_address(s
));
118 dma_unmap_sg(mdev
->sdev
->parent
, sg
, nents
, dir
);
122 static void __mic_dma_unmap_sg(struct device
*dev
,
123 struct scatterlist
*sg
, int nents
,
124 enum dma_data_direction dir
,
125 struct dma_attrs
*attrs
)
127 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
128 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
129 struct scatterlist
*s
;
133 for_each_sg(sg
, s
, nents
, i
) {
134 da
= mic_to_dma_addr(mdev
, sg_dma_address(s
));
135 mic_unmap(mdev
, sg_dma_address(s
), s
->length
);
136 sg_dma_address(s
) = da
;
138 dma_unmap_sg(mdev
->sdev
->parent
, sg
, nents
, dir
);
141 static struct dma_map_ops __mic_dma_ops
= {
142 .alloc
= __mic_dma_alloc
,
143 .free
= __mic_dma_free
,
144 .map_page
= __mic_dma_map_page
,
145 .unmap_page
= __mic_dma_unmap_page
,
146 .map_sg
= __mic_dma_map_sg
,
147 .unmap_sg
= __mic_dma_unmap_sg
,
150 static struct mic_irq
*
151 ___mic_request_irq(struct scif_hw_dev
*scdev
,
152 irqreturn_t (*func
)(int irq
, void *data
),
156 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
158 return mic_request_threaded_irq(mdev
, func
, NULL
, name
, data
,
163 ___mic_free_irq(struct scif_hw_dev
*scdev
,
164 struct mic_irq
*cookie
, void *data
)
166 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
168 return mic_free_irq(mdev
, cookie
, data
);
171 static void ___mic_ack_interrupt(struct scif_hw_dev
*scdev
, int num
)
173 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
175 mdev
->ops
->intr_workarounds(mdev
);
178 static int ___mic_next_db(struct scif_hw_dev
*scdev
)
180 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
182 return mic_next_db(mdev
);
185 static void ___mic_send_intr(struct scif_hw_dev
*scdev
, int db
)
187 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
189 mdev
->ops
->send_intr(mdev
, db
);
192 static void __iomem
*___mic_ioremap(struct scif_hw_dev
*scdev
,
193 phys_addr_t pa
, size_t len
)
195 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
197 return mdev
->aper
.va
+ pa
;
200 static void ___mic_iounmap(struct scif_hw_dev
*scdev
, void __iomem
*va
)
205 static struct scif_hw_ops scif_hw_ops
= {
206 .request_irq
= ___mic_request_irq
,
207 .free_irq
= ___mic_free_irq
,
208 .ack_interrupt
= ___mic_ack_interrupt
,
209 .next_db
= ___mic_next_db
,
210 .send_intr
= ___mic_send_intr
,
211 .ioremap
= ___mic_ioremap
,
212 .iounmap
= ___mic_iounmap
,
215 static inline struct mic_device
*mbdev_to_mdev(struct mbus_device
*mbdev
)
217 return dev_get_drvdata(mbdev
->dev
.parent
);
221 mic_dma_map_page(struct device
*dev
, struct page
*page
,
222 unsigned long offset
, size_t size
, enum dma_data_direction dir
,
223 struct dma_attrs
*attrs
)
225 void *va
= phys_to_virt(page_to_phys(page
)) + offset
;
226 struct mic_device
*mdev
= dev_get_drvdata(dev
->parent
);
228 return mic_map_single(mdev
, va
, size
);
232 mic_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
233 size_t size
, enum dma_data_direction dir
,
234 struct dma_attrs
*attrs
)
236 struct mic_device
*mdev
= dev_get_drvdata(dev
->parent
);
237 mic_unmap_single(mdev
, dma_addr
, size
);
240 static struct dma_map_ops mic_dma_ops
= {
241 .map_page
= mic_dma_map_page
,
242 .unmap_page
= mic_dma_unmap_page
,
245 static struct mic_irq
*
246 _mic_request_threaded_irq(struct mbus_device
*mbdev
,
247 irq_handler_t handler
, irq_handler_t thread_fn
,
248 const char *name
, void *data
, int intr_src
)
250 return mic_request_threaded_irq(mbdev_to_mdev(mbdev
), handler
,
251 thread_fn
, name
, data
,
252 intr_src
, MIC_INTR_DMA
);
255 static void _mic_free_irq(struct mbus_device
*mbdev
,
256 struct mic_irq
*cookie
, void *data
)
258 return mic_free_irq(mbdev_to_mdev(mbdev
), cookie
, data
);
261 static void _mic_ack_interrupt(struct mbus_device
*mbdev
, int num
)
263 struct mic_device
*mdev
= mbdev_to_mdev(mbdev
);
264 mdev
->ops
->intr_workarounds(mdev
);
267 static struct mbus_hw_ops mbus_hw_ops
= {
268 .request_threaded_irq
= _mic_request_threaded_irq
,
269 .free_irq
= _mic_free_irq
,
270 .ack_interrupt
= _mic_ack_interrupt
,
274 * mic_reset - Reset the MIC device.
275 * @mdev: pointer to mic_device instance
277 static void mic_reset(struct mic_device
*mdev
)
281 #define MIC_RESET_TO (45)
283 reinit_completion(&mdev
->reset_wait
);
284 mdev
->ops
->reset_fw_ready(mdev
);
285 mdev
->ops
->reset(mdev
);
287 for (i
= 0; i
< MIC_RESET_TO
; i
++) {
288 if (mdev
->ops
->is_fw_ready(mdev
))
291 * Resets typically take 10s of seconds to complete.
292 * Since an MMIO read is required to check if the
293 * firmware is ready or not, a 1 second delay works nicely.
297 mic_set_state(mdev
, MIC_RESET_FAILED
);
299 complete_all(&mdev
->reset_wait
);
302 /* Initialize the MIC bootparams */
303 void mic_bootparam_init(struct mic_device
*mdev
)
305 struct mic_bootparam
*bootparam
= mdev
->dp
;
307 bootparam
->magic
= cpu_to_le32(MIC_MAGIC
);
308 bootparam
->c2h_shutdown_db
= mdev
->shutdown_db
;
309 bootparam
->h2c_shutdown_db
= -1;
310 bootparam
->h2c_config_db
= -1;
311 bootparam
->shutdown_status
= 0;
312 bootparam
->shutdown_card
= 0;
313 /* Total nodes = number of MICs + 1 for self node */
314 bootparam
->tot_nodes
= atomic_read(&g_num_mics
) + 1;
315 bootparam
->node_id
= mdev
->id
+ 1;
316 bootparam
->scif_host_dma_addr
= 0x0;
317 bootparam
->scif_card_dma_addr
= 0x0;
318 bootparam
->c2h_scif_db
= -1;
319 bootparam
->h2c_scif_db
= -1;
323 * mic_request_dma_chans - Request DMA channels
324 * @mdev: pointer to mic_device instance
326 * returns number of DMA channels acquired
328 static int mic_request_dma_chans(struct mic_device
*mdev
)
331 struct dma_chan
*chan
;
333 request_module("mic_x100_dma");
335 dma_cap_set(DMA_MEMCPY
, mask
);
338 chan
= dma_request_channel(mask
, mdev
->ops
->dma_filter
,
341 mdev
->dma_ch
[mdev
->num_dma_ch
++] = chan
;
342 if (mdev
->num_dma_ch
>= MIC_MAX_DMA_CHAN
)
346 dev_info(mdev
->sdev
->parent
, "DMA channels # %d\n", mdev
->num_dma_ch
);
347 return mdev
->num_dma_ch
;
351 * mic_free_dma_chans - release DMA channels
352 * @mdev: pointer to mic_device instance
356 static void mic_free_dma_chans(struct mic_device
*mdev
)
360 for (i
= 0; i
< mdev
->num_dma_ch
; i
++) {
361 dma_release_channel(mdev
->dma_ch
[i
]);
362 mdev
->dma_ch
[i
] = NULL
;
364 mdev
->num_dma_ch
= 0;
368 * mic_start - Start the MIC.
369 * @mdev: pointer to mic_device instance
370 * @buf: buffer containing boot string including firmware/ramdisk path.
372 * This function prepares an MIC for boot and initiates boot.
373 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
375 int mic_start(struct mic_device
*mdev
, const char *buf
)
378 mutex_lock(&mdev
->mic_mutex
);
379 mic_bootparam_init(mdev
);
381 if (MIC_OFFLINE
!= mdev
->state
) {
385 if (!mdev
->ops
->is_fw_ready(mdev
)) {
388 * The state will either be MIC_OFFLINE if the reset succeeded
389 * or MIC_RESET_FAILED if the firmware reset failed.
393 mdev
->dma_mbdev
= mbus_register_device(mdev
->sdev
->parent
,
394 MBUS_DEV_DMA_HOST
, &mic_dma_ops
,
395 &mbus_hw_ops
, mdev
->mmio
.va
);
396 if (IS_ERR(mdev
->dma_mbdev
)) {
397 rc
= PTR_ERR(mdev
->dma_mbdev
);
400 if (!mic_request_dma_chans(mdev
)) {
404 mdev
->scdev
= scif_register_device(mdev
->sdev
->parent
, MIC_SCIF_DEV
,
405 &__mic_dma_ops
, &scif_hw_ops
,
406 mdev
->id
+ 1, 0, &mdev
->mmio
,
407 &mdev
->aper
, mdev
->dp
, NULL
,
408 mdev
->dma_ch
, mdev
->num_dma_ch
);
409 if (IS_ERR(mdev
->scdev
)) {
410 rc
= PTR_ERR(mdev
->scdev
);
413 rc
= mdev
->ops
->load_mic_fw(mdev
, buf
);
416 mic_smpt_restore(mdev
);
417 mic_intr_restore(mdev
);
418 mdev
->intr_ops
->enable_interrupts(mdev
);
419 mdev
->ops
->write_spad(mdev
, MIC_DPLO_SPAD
, mdev
->dp_dma_addr
);
420 mdev
->ops
->write_spad(mdev
, MIC_DPHI_SPAD
, mdev
->dp_dma_addr
>> 32);
421 mdev
->ops
->send_firmware_intr(mdev
);
422 mic_set_state(mdev
, MIC_ONLINE
);
425 scif_unregister_device(mdev
->scdev
);
427 mic_free_dma_chans(mdev
);
429 mbus_unregister_device(mdev
->dma_mbdev
);
431 mutex_unlock(&mdev
->mic_mutex
);
436 * mic_stop - Prepare the MIC for reset and trigger reset.
437 * @mdev: pointer to mic_device instance
438 * @force: force a MIC to reset even if it is already offline.
442 void mic_stop(struct mic_device
*mdev
, bool force
)
444 mutex_lock(&mdev
->mic_mutex
);
445 if (MIC_OFFLINE
!= mdev
->state
|| force
) {
446 scif_unregister_device(mdev
->scdev
);
447 mic_virtio_reset_devices(mdev
);
448 mic_free_dma_chans(mdev
);
449 mbus_unregister_device(mdev
->dma_mbdev
);
450 mic_bootparam_init(mdev
);
452 if (MIC_RESET_FAILED
== mdev
->state
)
454 mic_set_shutdown_status(mdev
, MIC_NOP
);
455 if (MIC_SUSPENDED
!= mdev
->state
)
456 mic_set_state(mdev
, MIC_OFFLINE
);
459 mutex_unlock(&mdev
->mic_mutex
);
463 * mic_shutdown - Initiate MIC shutdown.
464 * @mdev: pointer to mic_device instance
468 void mic_shutdown(struct mic_device
*mdev
)
470 struct mic_bootparam
*bootparam
= mdev
->dp
;
471 s8 db
= bootparam
->h2c_shutdown_db
;
473 mutex_lock(&mdev
->mic_mutex
);
474 if (MIC_ONLINE
== mdev
->state
&& db
!= -1) {
475 bootparam
->shutdown_card
= 1;
476 mdev
->ops
->send_intr(mdev
, db
);
477 mic_set_state(mdev
, MIC_SHUTTING_DOWN
);
479 mutex_unlock(&mdev
->mic_mutex
);
483 * mic_shutdown_work - Handle shutdown interrupt from MIC.
484 * @work: The work structure.
486 * This work is scheduled whenever the host has received a shutdown
487 * interrupt from the MIC.
489 void mic_shutdown_work(struct work_struct
*work
)
491 struct mic_device
*mdev
= container_of(work
, struct mic_device
,
493 struct mic_bootparam
*bootparam
= mdev
->dp
;
495 mutex_lock(&mdev
->mic_mutex
);
496 mic_set_shutdown_status(mdev
, bootparam
->shutdown_status
);
497 bootparam
->shutdown_status
= 0;
500 * if state is MIC_SUSPENDED, OSPM suspend is in progress. We do not
501 * change the state here so as to prevent users from booting the card
502 * during and after the suspend operation.
504 if (MIC_SHUTTING_DOWN
!= mdev
->state
&&
505 MIC_SUSPENDED
!= mdev
->state
)
506 mic_set_state(mdev
, MIC_SHUTTING_DOWN
);
507 mutex_unlock(&mdev
->mic_mutex
);
511 * mic_reset_trigger_work - Trigger MIC reset.
512 * @work: The work structure.
514 * This work is scheduled whenever the host wants to reset the MIC.
516 void mic_reset_trigger_work(struct work_struct
*work
)
518 struct mic_device
*mdev
= container_of(work
, struct mic_device
,
521 mic_stop(mdev
, false);
525 * mic_complete_resume - Complete MIC Resume after an OSPM suspend/hibernate
527 * @mdev: pointer to mic_device instance
531 void mic_complete_resume(struct mic_device
*mdev
)
533 if (mdev
->state
!= MIC_SUSPENDED
) {
534 dev_warn(mdev
->sdev
->parent
, "state %d should be %d\n",
535 mdev
->state
, MIC_SUSPENDED
);
539 /* Make sure firmware is ready */
540 if (!mdev
->ops
->is_fw_ready(mdev
))
541 mic_stop(mdev
, true);
543 mutex_lock(&mdev
->mic_mutex
);
544 mic_set_state(mdev
, MIC_OFFLINE
);
545 mutex_unlock(&mdev
->mic_mutex
);
549 * mic_prepare_suspend - Handle suspend notification for the MIC device.
550 * @mdev: pointer to mic_device instance
554 void mic_prepare_suspend(struct mic_device
*mdev
)
556 unsigned long timeout
;
558 #define MIC_SUSPEND_TIMEOUT (60 * HZ)
560 mutex_lock(&mdev
->mic_mutex
);
561 switch (mdev
->state
) {
564 * Card is already offline. Set state to MIC_SUSPENDED
565 * to prevent users from booting the card.
567 mic_set_state(mdev
, MIC_SUSPENDED
);
568 mutex_unlock(&mdev
->mic_mutex
);
572 * Card is online. Set state to MIC_SUSPENDING and notify
573 * MIC user space daemon which will issue card
574 * shutdown and reset.
576 mic_set_state(mdev
, MIC_SUSPENDING
);
577 mutex_unlock(&mdev
->mic_mutex
);
578 timeout
= wait_for_completion_timeout(&mdev
->reset_wait
,
579 MIC_SUSPEND_TIMEOUT
);
580 /* Force reset the card if the shutdown completion timed out */
582 mutex_lock(&mdev
->mic_mutex
);
583 mic_set_state(mdev
, MIC_SUSPENDED
);
584 mutex_unlock(&mdev
->mic_mutex
);
585 mic_stop(mdev
, true);
588 case MIC_SHUTTING_DOWN
:
590 * Card is shutting down. Set state to MIC_SUSPENDED
591 * to prevent further boot of the card.
593 mic_set_state(mdev
, MIC_SUSPENDED
);
594 mutex_unlock(&mdev
->mic_mutex
);
595 timeout
= wait_for_completion_timeout(&mdev
->reset_wait
,
596 MIC_SUSPEND_TIMEOUT
);
597 /* Force reset the card if the shutdown completion timed out */
599 mic_stop(mdev
, true);
602 mutex_unlock(&mdev
->mic_mutex
);
608 * mic_suspend - Initiate MIC suspend. Suspend merely issues card shutdown.
609 * @mdev: pointer to mic_device instance
613 void mic_suspend(struct mic_device
*mdev
)
615 struct mic_bootparam
*bootparam
= mdev
->dp
;
616 s8 db
= bootparam
->h2c_shutdown_db
;
618 mutex_lock(&mdev
->mic_mutex
);
619 if (MIC_SUSPENDING
== mdev
->state
&& db
!= -1) {
620 bootparam
->shutdown_card
= 1;
621 mdev
->ops
->send_intr(mdev
, db
);
622 mic_set_state(mdev
, MIC_SUSPENDED
);
624 mutex_unlock(&mdev
->mic_mutex
);