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>
25 #include <linux/mic_common.h>
26 #include <linux/mic_bus.h>
27 #include "../bus/scif_bus.h"
28 #include "../bus/vop_bus.h"
29 #include "../common/mic_dev.h"
30 #include "mic_device.h"
33 static inline struct mic_device
*vpdev_to_mdev(struct device
*dev
)
35 return dev_get_drvdata(dev
->parent
);
39 _mic_dma_map_page(struct device
*dev
, struct page
*page
,
40 unsigned long offset
, size_t size
,
41 enum dma_data_direction dir
, struct dma_attrs
*attrs
)
43 void *va
= phys_to_virt(page_to_phys(page
)) + offset
;
44 struct mic_device
*mdev
= vpdev_to_mdev(dev
);
46 return mic_map_single(mdev
, va
, size
);
49 static void _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
= vpdev_to_mdev(dev
);
55 mic_unmap_single(mdev
, dma_addr
, size
);
58 static const struct dma_map_ops _mic_dma_ops
= {
59 .map_page
= _mic_dma_map_page
,
60 .unmap_page
= _mic_dma_unmap_page
,
63 static struct mic_irq
*
64 __mic_request_irq(struct vop_device
*vpdev
,
65 irqreturn_t (*func
)(int irq
, void *data
),
66 const char *name
, void *data
, int intr_src
)
68 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
70 return mic_request_threaded_irq(mdev
, func
, NULL
, name
, data
,
71 intr_src
, MIC_INTR_DB
);
74 static void __mic_free_irq(struct vop_device
*vpdev
,
75 struct mic_irq
*cookie
, void *data
)
77 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
79 mic_free_irq(mdev
, cookie
, data
);
82 static void __mic_ack_interrupt(struct vop_device
*vpdev
, int num
)
84 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
86 mdev
->ops
->intr_workarounds(mdev
);
89 static int __mic_next_db(struct vop_device
*vpdev
)
91 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
93 return mic_next_db(mdev
);
96 static void *__mic_get_dp(struct vop_device
*vpdev
)
98 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
103 static void __iomem
*__mic_get_remote_dp(struct vop_device
*vpdev
)
108 static void __mic_send_intr(struct vop_device
*vpdev
, int db
)
110 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
112 mdev
->ops
->send_intr(mdev
, db
);
115 static void __iomem
*__mic_ioremap(struct vop_device
*vpdev
,
116 dma_addr_t pa
, size_t len
)
118 struct mic_device
*mdev
= vpdev_to_mdev(&vpdev
->dev
);
120 return mdev
->aper
.va
+ pa
;
123 static void __mic_iounmap(struct vop_device
*vpdev
, void __iomem
*va
)
128 static struct vop_hw_ops vop_hw_ops
= {
129 .request_irq
= __mic_request_irq
,
130 .free_irq
= __mic_free_irq
,
131 .ack_interrupt
= __mic_ack_interrupt
,
132 .next_db
= __mic_next_db
,
133 .get_dp
= __mic_get_dp
,
134 .get_remote_dp
= __mic_get_remote_dp
,
135 .send_intr
= __mic_send_intr
,
136 .ioremap
= __mic_ioremap
,
137 .iounmap
= __mic_iounmap
,
140 static inline struct mic_device
*scdev_to_mdev(struct scif_hw_dev
*scdev
)
142 return dev_get_drvdata(scdev
->dev
.parent
);
145 static void *__mic_dma_alloc(struct device
*dev
, size_t size
,
146 dma_addr_t
*dma_handle
, gfp_t gfp
,
147 struct dma_attrs
*attrs
)
149 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
150 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
152 void *va
= kmalloc(size
, gfp
);
155 tmp
= mic_map_single(mdev
, va
, size
);
156 if (dma_mapping_error(dev
, tmp
)) {
166 static void __mic_dma_free(struct device
*dev
, size_t size
, void *vaddr
,
167 dma_addr_t dma_handle
, struct dma_attrs
*attrs
)
169 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
170 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
172 mic_unmap_single(mdev
, dma_handle
, size
);
177 __mic_dma_map_page(struct device
*dev
, struct page
*page
, unsigned long offset
,
178 size_t size
, enum dma_data_direction dir
,
179 struct dma_attrs
*attrs
)
181 void *va
= phys_to_virt(page_to_phys(page
)) + offset
;
182 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
183 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
185 return mic_map_single(mdev
, va
, size
);
189 __mic_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
190 size_t size
, enum dma_data_direction dir
,
191 struct dma_attrs
*attrs
)
193 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
194 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
196 mic_unmap_single(mdev
, dma_addr
, size
);
199 static int __mic_dma_map_sg(struct device
*dev
, struct scatterlist
*sg
,
200 int nents
, enum dma_data_direction dir
,
201 struct dma_attrs
*attrs
)
203 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
204 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
205 struct scatterlist
*s
;
209 ret
= dma_map_sg(&mdev
->pdev
->dev
, sg
, nents
, dir
);
213 for_each_sg(sg
, s
, nents
, i
) {
214 da
= mic_map(mdev
, sg_dma_address(s
) + s
->offset
, s
->length
);
217 sg_dma_address(s
) = da
;
221 for_each_sg(sg
, s
, i
, j
) {
222 mic_unmap(mdev
, sg_dma_address(s
), s
->length
);
223 sg_dma_address(s
) = mic_to_dma_addr(mdev
, sg_dma_address(s
));
225 dma_unmap_sg(&mdev
->pdev
->dev
, sg
, nents
, dir
);
229 static void __mic_dma_unmap_sg(struct device
*dev
,
230 struct scatterlist
*sg
, int nents
,
231 enum dma_data_direction dir
,
232 struct dma_attrs
*attrs
)
234 struct scif_hw_dev
*scdev
= dev_get_drvdata(dev
);
235 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
236 struct scatterlist
*s
;
240 for_each_sg(sg
, s
, nents
, i
) {
241 da
= mic_to_dma_addr(mdev
, sg_dma_address(s
));
242 mic_unmap(mdev
, sg_dma_address(s
), s
->length
);
243 sg_dma_address(s
) = da
;
245 dma_unmap_sg(&mdev
->pdev
->dev
, sg
, nents
, dir
);
248 static struct dma_map_ops __mic_dma_ops
= {
249 .alloc
= __mic_dma_alloc
,
250 .free
= __mic_dma_free
,
251 .map_page
= __mic_dma_map_page
,
252 .unmap_page
= __mic_dma_unmap_page
,
253 .map_sg
= __mic_dma_map_sg
,
254 .unmap_sg
= __mic_dma_unmap_sg
,
257 static struct mic_irq
*
258 ___mic_request_irq(struct scif_hw_dev
*scdev
,
259 irqreturn_t (*func
)(int irq
, void *data
),
263 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
265 return mic_request_threaded_irq(mdev
, func
, NULL
, name
, data
,
270 ___mic_free_irq(struct scif_hw_dev
*scdev
,
271 struct mic_irq
*cookie
, void *data
)
273 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
275 mic_free_irq(mdev
, cookie
, data
);
278 static void ___mic_ack_interrupt(struct scif_hw_dev
*scdev
, int num
)
280 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
282 mdev
->ops
->intr_workarounds(mdev
);
285 static int ___mic_next_db(struct scif_hw_dev
*scdev
)
287 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
289 return mic_next_db(mdev
);
292 static void ___mic_send_intr(struct scif_hw_dev
*scdev
, int db
)
294 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
296 mdev
->ops
->send_intr(mdev
, db
);
299 static void __iomem
*___mic_ioremap(struct scif_hw_dev
*scdev
,
300 phys_addr_t pa
, size_t len
)
302 struct mic_device
*mdev
= scdev_to_mdev(scdev
);
304 return mdev
->aper
.va
+ pa
;
307 static void ___mic_iounmap(struct scif_hw_dev
*scdev
, void __iomem
*va
)
312 static struct scif_hw_ops scif_hw_ops
= {
313 .request_irq
= ___mic_request_irq
,
314 .free_irq
= ___mic_free_irq
,
315 .ack_interrupt
= ___mic_ack_interrupt
,
316 .next_db
= ___mic_next_db
,
317 .send_intr
= ___mic_send_intr
,
318 .ioremap
= ___mic_ioremap
,
319 .iounmap
= ___mic_iounmap
,
322 static inline struct mic_device
*mbdev_to_mdev(struct mbus_device
*mbdev
)
324 return dev_get_drvdata(mbdev
->dev
.parent
);
328 mic_dma_map_page(struct device
*dev
, struct page
*page
,
329 unsigned long offset
, size_t size
, enum dma_data_direction dir
,
330 struct dma_attrs
*attrs
)
332 void *va
= phys_to_virt(page_to_phys(page
)) + offset
;
333 struct mic_device
*mdev
= dev_get_drvdata(dev
->parent
);
335 return mic_map_single(mdev
, va
, size
);
339 mic_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
340 size_t size
, enum dma_data_direction dir
,
341 struct dma_attrs
*attrs
)
343 struct mic_device
*mdev
= dev_get_drvdata(dev
->parent
);
344 mic_unmap_single(mdev
, dma_addr
, size
);
347 static struct dma_map_ops mic_dma_ops
= {
348 .map_page
= mic_dma_map_page
,
349 .unmap_page
= mic_dma_unmap_page
,
352 static struct mic_irq
*
353 _mic_request_threaded_irq(struct mbus_device
*mbdev
,
354 irq_handler_t handler
, irq_handler_t thread_fn
,
355 const char *name
, void *data
, int intr_src
)
357 return mic_request_threaded_irq(mbdev_to_mdev(mbdev
), handler
,
358 thread_fn
, name
, data
,
359 intr_src
, MIC_INTR_DMA
);
362 static void _mic_free_irq(struct mbus_device
*mbdev
,
363 struct mic_irq
*cookie
, void *data
)
365 mic_free_irq(mbdev_to_mdev(mbdev
), cookie
, data
);
368 static void _mic_ack_interrupt(struct mbus_device
*mbdev
, int num
)
370 struct mic_device
*mdev
= mbdev_to_mdev(mbdev
);
371 mdev
->ops
->intr_workarounds(mdev
);
374 static struct mbus_hw_ops mbus_hw_ops
= {
375 .request_threaded_irq
= _mic_request_threaded_irq
,
376 .free_irq
= _mic_free_irq
,
377 .ack_interrupt
= _mic_ack_interrupt
,
380 /* Initialize the MIC bootparams */
381 void mic_bootparam_init(struct mic_device
*mdev
)
383 struct mic_bootparam
*bootparam
= mdev
->dp
;
385 bootparam
->magic
= cpu_to_le32(MIC_MAGIC
);
386 bootparam
->h2c_config_db
= -1;
387 bootparam
->node_id
= mdev
->id
+ 1;
388 bootparam
->scif_host_dma_addr
= 0x0;
389 bootparam
->scif_card_dma_addr
= 0x0;
390 bootparam
->c2h_scif_db
= -1;
391 bootparam
->h2c_scif_db
= -1;
394 static inline struct mic_device
*cosmdev_to_mdev(struct cosm_device
*cdev
)
396 return dev_get_drvdata(cdev
->dev
.parent
);
399 static void _mic_reset(struct cosm_device
*cdev
)
401 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
403 mdev
->ops
->reset_fw_ready(mdev
);
404 mdev
->ops
->reset(mdev
);
407 static bool _mic_ready(struct cosm_device
*cdev
)
409 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
411 return mdev
->ops
->is_fw_ready(mdev
);
415 * mic_request_dma_chans - Request DMA channels
416 * @mdev: pointer to mic_device instance
418 * returns number of DMA channels acquired
420 static int mic_request_dma_chans(struct mic_device
*mdev
)
423 struct dma_chan
*chan
;
426 dma_cap_set(DMA_MEMCPY
, mask
);
429 chan
= dma_request_channel(mask
, mdev
->ops
->dma_filter
,
432 mdev
->dma_ch
[mdev
->num_dma_ch
++] = chan
;
433 if (mdev
->num_dma_ch
>= MIC_MAX_DMA_CHAN
)
437 dev_info(&mdev
->pdev
->dev
, "DMA channels # %d\n", mdev
->num_dma_ch
);
438 return mdev
->num_dma_ch
;
442 * mic_free_dma_chans - release DMA channels
443 * @mdev: pointer to mic_device instance
447 static void mic_free_dma_chans(struct mic_device
*mdev
)
451 for (i
= 0; i
< mdev
->num_dma_ch
; i
++) {
452 dma_release_channel(mdev
->dma_ch
[i
]);
453 mdev
->dma_ch
[i
] = NULL
;
455 mdev
->num_dma_ch
= 0;
459 * _mic_start - Start the MIC.
460 * @cdev: pointer to cosm_device instance
461 * @id: MIC device id/index provided by COSM used in other drivers like SCIF
463 * This function prepares an MIC for boot and initiates boot.
464 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
466 * For all cosm_hw_ops the caller holds a mutex to ensure serialization.
468 static int _mic_start(struct cosm_device
*cdev
, int id
)
470 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
473 mic_bootparam_init(mdev
);
474 mdev
->dma_mbdev
= mbus_register_device(&mdev
->pdev
->dev
,
475 MBUS_DEV_DMA_HOST
, &mic_dma_ops
,
476 &mbus_hw_ops
, id
, mdev
->mmio
.va
);
477 if (IS_ERR(mdev
->dma_mbdev
)) {
478 rc
= PTR_ERR(mdev
->dma_mbdev
);
481 if (!mic_request_dma_chans(mdev
)) {
485 mdev
->scdev
= scif_register_device(&mdev
->pdev
->dev
, MIC_SCIF_DEV
,
486 &__mic_dma_ops
, &scif_hw_ops
,
487 id
+ 1, 0, &mdev
->mmio
,
488 &mdev
->aper
, mdev
->dp
, NULL
,
489 mdev
->dma_ch
, mdev
->num_dma_ch
,
491 if (IS_ERR(mdev
->scdev
)) {
492 rc
= PTR_ERR(mdev
->scdev
);
496 mdev
->vpdev
= vop_register_device(&mdev
->pdev
->dev
,
497 VOP_DEV_TRNSP
, &_mic_dma_ops
,
498 &vop_hw_ops
, id
+ 1, &mdev
->aper
,
500 if (IS_ERR(mdev
->vpdev
)) {
501 rc
= PTR_ERR(mdev
->vpdev
);
505 rc
= mdev
->ops
->load_mic_fw(mdev
, NULL
);
508 mic_smpt_restore(mdev
);
509 mic_intr_restore(mdev
);
510 mdev
->intr_ops
->enable_interrupts(mdev
);
511 mdev
->ops
->write_spad(mdev
, MIC_DPLO_SPAD
, mdev
->dp_dma_addr
);
512 mdev
->ops
->write_spad(mdev
, MIC_DPHI_SPAD
, mdev
->dp_dma_addr
>> 32);
513 mdev
->ops
->send_firmware_intr(mdev
);
516 vop_unregister_device(mdev
->vpdev
);
518 scif_unregister_device(mdev
->scdev
);
520 mic_free_dma_chans(mdev
);
522 mbus_unregister_device(mdev
->dma_mbdev
);
528 * _mic_stop - Prepare the MIC for reset and trigger reset.
529 * @cdev: pointer to cosm_device instance
530 * @force: force a MIC to reset even if it is already offline.
534 static void _mic_stop(struct cosm_device
*cdev
, bool force
)
536 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
539 * Since SCIF handles card shutdown and reset (using COSM), it will
540 * will be the first to be registered and the last to be
543 vop_unregister_device(mdev
->vpdev
);
544 scif_unregister_device(mdev
->scdev
);
545 mic_free_dma_chans(mdev
);
546 mbus_unregister_device(mdev
->dma_mbdev
);
547 mic_bootparam_init(mdev
);
550 static ssize_t
_mic_family(struct cosm_device
*cdev
, char *buf
)
552 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
553 static const char *family
[MIC_FAMILY_LAST
] = { "x100", "Unknown" };
555 return scnprintf(buf
, PAGE_SIZE
, "%s\n", family
[mdev
->family
]);
558 static ssize_t
_mic_stepping(struct cosm_device
*cdev
, char *buf
)
560 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
561 const char *string
= "??";
563 switch (mdev
->stepping
) {
579 return scnprintf(buf
, PAGE_SIZE
, "%s\n", string
);
582 static struct mic_mw
*_mic_aper(struct cosm_device
*cdev
)
584 struct mic_device
*mdev
= cosmdev_to_mdev(cdev
);
589 struct cosm_hw_ops cosm_hw_ops
= {
591 .force_reset
= _mic_reset
,
596 .family
= _mic_family
,
597 .stepping
= _mic_stepping
,