1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2013 Intel Corporation.
7 * Disclaimer: The codes contained in these modules may be specific to
8 * the Intel Software Development Platform codenamed: Knights Ferry, and
9 * the Intel product codenamed: Knights Corner, and are not backward
10 * compatible with other Intel products. Additionally, Intel will NOT
11 * support the codes or instruction set in future products.
13 * Intel MIC Card driver.
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/interrupt.h>
18 #include <linux/reboot.h>
19 #include <linux/dmaengine.h>
20 #include <linux/kmod.h>
22 #include <linux/mic_common.h>
23 #include "../common/mic_dev.h"
24 #include "mic_device.h"
26 static struct mic_driver
*g_drv
;
28 static int __init
mic_dp_init(void)
30 struct mic_driver
*mdrv
= g_drv
;
31 struct mic_device
*mdev
= &mdrv
->mdev
;
32 struct mic_bootparam __iomem
*bootparam
;
33 u64 lo
, hi
, dp_dma_addr
;
36 lo
= mic_read_spad(&mdrv
->mdev
, MIC_DPLO_SPAD
);
37 hi
= mic_read_spad(&mdrv
->mdev
, MIC_DPHI_SPAD
);
39 dp_dma_addr
= lo
| (hi
<< 32);
40 mdrv
->dp
= mic_card_map(mdev
, dp_dma_addr
, MIC_DP_SIZE
);
42 dev_err(mdrv
->dev
, "Cannot remap Aperture BAR\n");
46 magic
= ioread32(&bootparam
->magic
);
47 if (MIC_MAGIC
!= magic
) {
48 dev_err(mdrv
->dev
, "bootparam magic mismatch 0x%x\n", magic
);
54 /* Uninitialize the device page */
55 static void mic_dp_uninit(void)
57 mic_card_unmap(&g_drv
->mdev
, g_drv
->dp
);
61 * mic_request_card_irq - request an irq.
63 * @handler: interrupt handler passed to request_threaded_irq.
64 * @thread_fn: thread fn. passed to request_threaded_irq.
65 * @name: The ASCII name of the callee requesting the irq.
66 * @data: private data that is returned back when calling the
68 * @index: The doorbell index of the requester.
70 * returns: The cookie that is transparent to the caller. Passed
71 * back when calling mic_free_irq. An appropriate error code
72 * is returned on failure. Caller needs to use IS_ERR(return_val)
73 * to check for failure and PTR_ERR(return_val) to obtained the
78 mic_request_card_irq(irq_handler_t handler
,
79 irq_handler_t thread_fn
, const char *name
,
80 void *data
, int index
)
84 struct mic_driver
*mdrv
= g_drv
;
86 rc
= request_threaded_irq(mic_db_to_irq(mdrv
, index
), handler
,
87 thread_fn
, 0, name
, data
);
89 dev_err(mdrv
->dev
, "request_threaded_irq failed rc = %d\n", rc
);
92 mdrv
->irq_info
.irq_usage_count
[index
]++;
94 return (struct mic_irq
*)cookie
;
100 * mic_free_card_irq - free irq.
102 * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
103 * @data: private data specified by the calling function during the
104 * mic_request_threaded_irq
108 void mic_free_card_irq(struct mic_irq
*cookie
, void *data
)
111 struct mic_driver
*mdrv
= g_drv
;
113 index
= (unsigned long)cookie
& 0xFFFFU
;
114 free_irq(mic_db_to_irq(mdrv
, index
), data
);
115 mdrv
->irq_info
.irq_usage_count
[index
]--;
119 * mic_next_card_db - Get the doorbell with minimum usage count.
121 * Returns the irq index.
123 int mic_next_card_db(void)
127 struct mic_driver
*mdrv
= g_drv
;
129 for (i
= 0; i
< mdrv
->intr_info
.num_intr
; i
++) {
130 if (mdrv
->irq_info
.irq_usage_count
[i
] <
131 mdrv
->irq_info
.irq_usage_count
[index
])
139 * mic_init_irq - Initialize irq information.
141 * Returns 0 in success. Appropriate error code on failure.
143 static int mic_init_irq(void)
145 struct mic_driver
*mdrv
= g_drv
;
147 mdrv
->irq_info
.irq_usage_count
= kzalloc((sizeof(u32
) *
148 mdrv
->intr_info
.num_intr
),
150 if (!mdrv
->irq_info
.irq_usage_count
)
156 * mic_uninit_irq - Uninitialize irq information.
160 static void mic_uninit_irq(void)
162 struct mic_driver
*mdrv
= g_drv
;
164 kfree(mdrv
->irq_info
.irq_usage_count
);
167 static inline struct mic_driver
*scdev_to_mdrv(struct scif_hw_dev
*scdev
)
169 return dev_get_drvdata(scdev
->dev
.parent
);
172 static struct mic_irq
*
173 ___mic_request_irq(struct scif_hw_dev
*scdev
,
174 irqreturn_t (*func
)(int irq
, void *data
),
175 const char *name
, void *data
,
178 return mic_request_card_irq(func
, NULL
, name
, data
, db
);
182 ___mic_free_irq(struct scif_hw_dev
*scdev
,
183 struct mic_irq
*cookie
, void *data
)
185 return mic_free_card_irq(cookie
, data
);
188 static void ___mic_ack_interrupt(struct scif_hw_dev
*scdev
, int num
)
190 struct mic_driver
*mdrv
= scdev_to_mdrv(scdev
);
192 mic_ack_interrupt(&mdrv
->mdev
);
195 static int ___mic_next_db(struct scif_hw_dev
*scdev
)
197 return mic_next_card_db();
200 static void ___mic_send_intr(struct scif_hw_dev
*scdev
, int db
)
202 struct mic_driver
*mdrv
= scdev_to_mdrv(scdev
);
204 mic_send_intr(&mdrv
->mdev
, db
);
207 static void ___mic_send_p2p_intr(struct scif_hw_dev
*scdev
, int db
,
210 mic_send_p2p_intr(db
, mw
);
213 static void __iomem
*
214 ___mic_ioremap(struct scif_hw_dev
*scdev
,
215 phys_addr_t pa
, size_t len
)
217 struct mic_driver
*mdrv
= scdev_to_mdrv(scdev
);
219 return mic_card_map(&mdrv
->mdev
, pa
, len
);
222 static void ___mic_iounmap(struct scif_hw_dev
*scdev
, void __iomem
*va
)
224 struct mic_driver
*mdrv
= scdev_to_mdrv(scdev
);
226 mic_card_unmap(&mdrv
->mdev
, va
);
229 static struct scif_hw_ops scif_hw_ops
= {
230 .request_irq
= ___mic_request_irq
,
231 .free_irq
= ___mic_free_irq
,
232 .ack_interrupt
= ___mic_ack_interrupt
,
233 .next_db
= ___mic_next_db
,
234 .send_intr
= ___mic_send_intr
,
235 .send_p2p_intr
= ___mic_send_p2p_intr
,
236 .remap
= ___mic_ioremap
,
237 .unmap
= ___mic_iounmap
,
240 static inline struct mic_driver
*vpdev_to_mdrv(struct vop_device
*vpdev
)
242 return dev_get_drvdata(vpdev
->dev
.parent
);
245 static struct mic_irq
*
246 __mic_request_irq(struct vop_device
*vpdev
,
247 irqreturn_t (*func
)(int irq
, void *data
),
248 const char *name
, void *data
, int intr_src
)
250 return mic_request_card_irq(func
, NULL
, name
, data
, intr_src
);
253 static void __mic_free_irq(struct vop_device
*vpdev
,
254 struct mic_irq
*cookie
, void *data
)
256 return mic_free_card_irq(cookie
, data
);
259 static void __mic_ack_interrupt(struct vop_device
*vpdev
, int num
)
261 struct mic_driver
*mdrv
= vpdev_to_mdrv(vpdev
);
263 mic_ack_interrupt(&mdrv
->mdev
);
266 static int __mic_next_db(struct vop_device
*vpdev
)
268 return mic_next_card_db();
271 static void __iomem
*__mic_get_remote_dp(struct vop_device
*vpdev
)
273 struct mic_driver
*mdrv
= vpdev_to_mdrv(vpdev
);
278 static void __mic_send_intr(struct vop_device
*vpdev
, int db
)
280 struct mic_driver
*mdrv
= vpdev_to_mdrv(vpdev
);
282 mic_send_intr(&mdrv
->mdev
, db
);
285 static void __iomem
*__mic_ioremap(struct vop_device
*vpdev
,
286 dma_addr_t pa
, size_t len
)
288 struct mic_driver
*mdrv
= vpdev_to_mdrv(vpdev
);
290 return mic_card_map(&mdrv
->mdev
, pa
, len
);
293 static void __mic_iounmap(struct vop_device
*vpdev
, void __iomem
*va
)
295 struct mic_driver
*mdrv
= vpdev_to_mdrv(vpdev
);
297 mic_card_unmap(&mdrv
->mdev
, va
);
300 static struct vop_hw_ops vop_hw_ops
= {
301 .request_irq
= __mic_request_irq
,
302 .free_irq
= __mic_free_irq
,
303 .ack_interrupt
= __mic_ack_interrupt
,
304 .next_db
= __mic_next_db
,
305 .get_remote_dp
= __mic_get_remote_dp
,
306 .send_intr
= __mic_send_intr
,
307 .remap
= __mic_ioremap
,
308 .unmap
= __mic_iounmap
,
311 static int mic_request_dma_chans(struct mic_driver
*mdrv
)
314 struct dma_chan
*chan
;
317 dma_cap_set(DMA_MEMCPY
, mask
);
320 chan
= dma_request_channel(mask
, NULL
, NULL
);
322 mdrv
->dma_ch
[mdrv
->num_dma_ch
++] = chan
;
323 if (mdrv
->num_dma_ch
>= MIC_MAX_DMA_CHAN
)
327 dev_info(mdrv
->dev
, "DMA channels # %d\n", mdrv
->num_dma_ch
);
328 return mdrv
->num_dma_ch
;
331 static void mic_free_dma_chans(struct mic_driver
*mdrv
)
335 for (i
= 0; i
< mdrv
->num_dma_ch
; i
++) {
336 dma_release_channel(mdrv
->dma_ch
[i
]);
337 mdrv
->dma_ch
[i
] = NULL
;
339 mdrv
->num_dma_ch
= 0;
343 * mic_driver_init - MIC driver initialization tasks.
345 * Returns 0 in success. Appropriate error code on failure.
347 int __init
mic_driver_init(struct mic_driver
*mdrv
)
350 struct mic_bootparam __iomem
*bootparam
;
354 /* Unloading the card module is not supported. */
355 if (!try_module_get(mdrv
->dev
->driver
->owner
)) {
365 if (!mic_request_dma_chans(mdrv
)) {
369 mdrv
->vpdev
= vop_register_device(mdrv
->dev
, VOP_DEV_TRNSP
,
370 NULL
, &vop_hw_ops
, 0,
371 NULL
, mdrv
->dma_ch
[0]);
372 if (IS_ERR(mdrv
->vpdev
)) {
373 rc
= PTR_ERR(mdrv
->vpdev
);
376 bootparam
= mdrv
->dp
;
377 node_id
= ioread8(&bootparam
->node_id
);
378 mdrv
->scdev
= scif_register_device(mdrv
->dev
, MIC_SCIF_DEV
,
380 0, node_id
, &mdrv
->mdev
.mmio
, NULL
,
381 NULL
, mdrv
->dp
, mdrv
->dma_ch
,
382 mdrv
->num_dma_ch
, true);
383 if (IS_ERR(mdrv
->scdev
)) {
384 rc
= PTR_ERR(mdrv
->scdev
);
387 mic_create_card_debug_dir(mdrv
);
391 vop_unregister_device(mdrv
->vpdev
);
393 mic_free_dma_chans(mdrv
);
399 module_put(mdrv
->dev
->driver
->owner
);
404 * mic_driver_uninit - MIC driver uninitialization tasks.
408 void mic_driver_uninit(struct mic_driver
*mdrv
)
410 mic_delete_card_debug_dir(mdrv
);
411 scif_unregister_device(mdrv
->scdev
);
412 vop_unregister_device(mdrv
->vpdev
);
413 mic_free_dma_chans(mdrv
);
416 module_put(mdrv
->dev
->driver
->owner
);