Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / mic / card / mic_device.c
blobdcd07ef298017c23d06185a629a50254129eaeea
1 /*
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 * Disclaimer: The codes contained in these modules may be specific to
19 * the Intel Software Development Platform codenamed: Knights Ferry, and
20 * the Intel product codenamed: Knights Corner, and are not backward
21 * compatible with other Intel products. Additionally, Intel will NOT
22 * support the codes or instruction set in future products.
24 * Intel MIC Card driver.
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/interrupt.h>
30 #include <linux/reboot.h>
31 #include <linux/dmaengine.h>
32 #include <linux/kmod.h>
34 #include <linux/mic_common.h>
35 #include "../common/mic_dev.h"
36 #include "mic_device.h"
38 static struct mic_driver *g_drv;
40 static int __init mic_dp_init(void)
42 struct mic_driver *mdrv = g_drv;
43 struct mic_device *mdev = &mdrv->mdev;
44 struct mic_bootparam __iomem *bootparam;
45 u64 lo, hi, dp_dma_addr;
46 u32 magic;
48 lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
49 hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
51 dp_dma_addr = lo | (hi << 32);
52 mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
53 if (!mdrv->dp) {
54 dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
55 return -ENOMEM;
57 bootparam = mdrv->dp;
58 magic = ioread32(&bootparam->magic);
59 if (MIC_MAGIC != magic) {
60 dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
61 return -EIO;
63 return 0;
66 /* Uninitialize the device page */
67 static void mic_dp_uninit(void)
69 mic_card_unmap(&g_drv->mdev, g_drv->dp);
72 /**
73 * mic_request_card_irq - request an irq.
75 * @handler: interrupt handler passed to request_threaded_irq.
76 * @thread_fn: thread fn. passed to request_threaded_irq.
77 * @name: The ASCII name of the callee requesting the irq.
78 * @data: private data that is returned back when calling the
79 * function handler.
80 * @index: The doorbell index of the requester.
82 * returns: The cookie that is transparent to the caller. Passed
83 * back when calling mic_free_irq. An appropriate error code
84 * is returned on failure. Caller needs to use IS_ERR(return_val)
85 * to check for failure and PTR_ERR(return_val) to obtained the
86 * error code.
89 struct mic_irq *
90 mic_request_card_irq(irq_handler_t handler,
91 irq_handler_t thread_fn, const char *name,
92 void *data, int index)
94 int rc = 0;
95 unsigned long cookie;
96 struct mic_driver *mdrv = g_drv;
98 rc = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
99 thread_fn, 0, name, data);
100 if (rc) {
101 dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
102 goto err;
104 mdrv->irq_info.irq_usage_count[index]++;
105 cookie = index;
106 return (struct mic_irq *)cookie;
107 err:
108 return ERR_PTR(rc);
112 * mic_free_card_irq - free irq.
114 * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
115 * @data: private data specified by the calling function during the
116 * mic_request_threaded_irq
118 * returns: none.
120 void mic_free_card_irq(struct mic_irq *cookie, void *data)
122 int index;
123 struct mic_driver *mdrv = g_drv;
125 index = (unsigned long)cookie & 0xFFFFU;
126 free_irq(mic_db_to_irq(mdrv, index), data);
127 mdrv->irq_info.irq_usage_count[index]--;
131 * mic_next_card_db - Get the doorbell with minimum usage count.
133 * Returns the irq index.
135 int mic_next_card_db(void)
137 int i;
138 int index = 0;
139 struct mic_driver *mdrv = g_drv;
141 for (i = 0; i < mdrv->intr_info.num_intr; i++) {
142 if (mdrv->irq_info.irq_usage_count[i] <
143 mdrv->irq_info.irq_usage_count[index])
144 index = i;
147 return index;
151 * mic_init_irq - Initialize irq information.
153 * Returns 0 in success. Appropriate error code on failure.
155 static int mic_init_irq(void)
157 struct mic_driver *mdrv = g_drv;
159 mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
160 mdrv->intr_info.num_intr),
161 GFP_KERNEL);
162 if (!mdrv->irq_info.irq_usage_count)
163 return -ENOMEM;
164 return 0;
168 * mic_uninit_irq - Uninitialize irq information.
170 * None.
172 static void mic_uninit_irq(void)
174 struct mic_driver *mdrv = g_drv;
176 kfree(mdrv->irq_info.irq_usage_count);
179 static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
181 return dev_get_drvdata(scdev->dev.parent);
184 static struct mic_irq *
185 ___mic_request_irq(struct scif_hw_dev *scdev,
186 irqreturn_t (*func)(int irq, void *data),
187 const char *name, void *data,
188 int db)
190 return mic_request_card_irq(func, NULL, name, data, db);
193 static void
194 ___mic_free_irq(struct scif_hw_dev *scdev,
195 struct mic_irq *cookie, void *data)
197 return mic_free_card_irq(cookie, data);
200 static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
202 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
204 mic_ack_interrupt(&mdrv->mdev);
207 static int ___mic_next_db(struct scif_hw_dev *scdev)
209 return mic_next_card_db();
212 static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
214 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
216 mic_send_intr(&mdrv->mdev, db);
219 static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
220 struct mic_mw *mw)
222 mic_send_p2p_intr(db, mw);
225 static void __iomem *
226 ___mic_ioremap(struct scif_hw_dev *scdev,
227 phys_addr_t pa, size_t len)
229 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
231 return mic_card_map(&mdrv->mdev, pa, len);
234 static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
236 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
238 mic_card_unmap(&mdrv->mdev, va);
241 static struct scif_hw_ops scif_hw_ops = {
242 .request_irq = ___mic_request_irq,
243 .free_irq = ___mic_free_irq,
244 .ack_interrupt = ___mic_ack_interrupt,
245 .next_db = ___mic_next_db,
246 .send_intr = ___mic_send_intr,
247 .send_p2p_intr = ___mic_send_p2p_intr,
248 .remap = ___mic_ioremap,
249 .unmap = ___mic_iounmap,
252 static inline struct mic_driver *vpdev_to_mdrv(struct vop_device *vpdev)
254 return dev_get_drvdata(vpdev->dev.parent);
257 static struct mic_irq *
258 __mic_request_irq(struct vop_device *vpdev,
259 irqreturn_t (*func)(int irq, void *data),
260 const char *name, void *data, int intr_src)
262 return mic_request_card_irq(func, NULL, name, data, intr_src);
265 static void __mic_free_irq(struct vop_device *vpdev,
266 struct mic_irq *cookie, void *data)
268 return mic_free_card_irq(cookie, data);
271 static void __mic_ack_interrupt(struct vop_device *vpdev, int num)
273 struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
275 mic_ack_interrupt(&mdrv->mdev);
278 static int __mic_next_db(struct vop_device *vpdev)
280 return mic_next_card_db();
283 static void __iomem *__mic_get_remote_dp(struct vop_device *vpdev)
285 struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
287 return mdrv->dp;
290 static void __mic_send_intr(struct vop_device *vpdev, int db)
292 struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
294 mic_send_intr(&mdrv->mdev, db);
297 static void __iomem *__mic_ioremap(struct vop_device *vpdev,
298 dma_addr_t pa, size_t len)
300 struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
302 return mic_card_map(&mdrv->mdev, pa, len);
305 static void __mic_iounmap(struct vop_device *vpdev, void __iomem *va)
307 struct mic_driver *mdrv = vpdev_to_mdrv(vpdev);
309 mic_card_unmap(&mdrv->mdev, va);
312 static struct vop_hw_ops vop_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 .get_remote_dp = __mic_get_remote_dp,
318 .send_intr = __mic_send_intr,
319 .remap = __mic_ioremap,
320 .unmap = __mic_iounmap,
323 static int mic_request_dma_chans(struct mic_driver *mdrv)
325 dma_cap_mask_t mask;
326 struct dma_chan *chan;
328 dma_cap_zero(mask);
329 dma_cap_set(DMA_MEMCPY, mask);
331 do {
332 chan = dma_request_channel(mask, NULL, NULL);
333 if (chan) {
334 mdrv->dma_ch[mdrv->num_dma_ch++] = chan;
335 if (mdrv->num_dma_ch >= MIC_MAX_DMA_CHAN)
336 break;
338 } while (chan);
339 dev_info(mdrv->dev, "DMA channels # %d\n", mdrv->num_dma_ch);
340 return mdrv->num_dma_ch;
343 static void mic_free_dma_chans(struct mic_driver *mdrv)
345 int i = 0;
347 for (i = 0; i < mdrv->num_dma_ch; i++) {
348 dma_release_channel(mdrv->dma_ch[i]);
349 mdrv->dma_ch[i] = NULL;
351 mdrv->num_dma_ch = 0;
355 * mic_driver_init - MIC driver initialization tasks.
357 * Returns 0 in success. Appropriate error code on failure.
359 int __init mic_driver_init(struct mic_driver *mdrv)
361 int rc;
362 struct mic_bootparam __iomem *bootparam;
363 u8 node_id;
365 g_drv = mdrv;
366 /* Unloading the card module is not supported. */
367 if (!try_module_get(mdrv->dev->driver->owner)) {
368 rc = -ENODEV;
369 goto done;
371 rc = mic_dp_init();
372 if (rc)
373 goto put;
374 rc = mic_init_irq();
375 if (rc)
376 goto dp_uninit;
377 if (!mic_request_dma_chans(mdrv)) {
378 rc = -ENODEV;
379 goto irq_uninit;
381 mdrv->vpdev = vop_register_device(mdrv->dev, VOP_DEV_TRNSP,
382 NULL, &vop_hw_ops, 0,
383 NULL, mdrv->dma_ch[0]);
384 if (IS_ERR(mdrv->vpdev)) {
385 rc = PTR_ERR(mdrv->vpdev);
386 goto dma_free;
388 bootparam = mdrv->dp;
389 node_id = ioread8(&bootparam->node_id);
390 mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
391 NULL, &scif_hw_ops,
392 0, node_id, &mdrv->mdev.mmio, NULL,
393 NULL, mdrv->dp, mdrv->dma_ch,
394 mdrv->num_dma_ch, true);
395 if (IS_ERR(mdrv->scdev)) {
396 rc = PTR_ERR(mdrv->scdev);
397 goto vop_remove;
399 mic_create_card_debug_dir(mdrv);
400 done:
401 return rc;
402 vop_remove:
403 vop_unregister_device(mdrv->vpdev);
404 dma_free:
405 mic_free_dma_chans(mdrv);
406 irq_uninit:
407 mic_uninit_irq();
408 dp_uninit:
409 mic_dp_uninit();
410 put:
411 module_put(mdrv->dev->driver->owner);
412 return rc;
416 * mic_driver_uninit - MIC driver uninitialization tasks.
418 * Returns None
420 void mic_driver_uninit(struct mic_driver *mdrv)
422 mic_delete_card_debug_dir(mdrv);
423 scif_unregister_device(mdrv->scdev);
424 vop_unregister_device(mdrv->vpdev);
425 mic_free_dma_chans(mdrv);
426 mic_uninit_irq();
427 mic_dp_uninit();
428 module_put(mdrv->dev->driver->owner);