These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / misc / mic / card / mic_device.c
1 /*
2  * Intel MIC Platform Software Stack (MPSS)
3  *
4  * Copyright(c) 2013 Intel Corporation.
5  *
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.
9  *
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.
14  *
15  * The full GNU General Public License is included in this distribution in
16  * the file called "COPYING".
17  *
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.
23  *
24  * Intel MIC Card driver.
25  *
26  */
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>
33
34 #include <linux/mic_common.h>
35 #include "../common/mic_dev.h"
36 #include "mic_device.h"
37 #include "mic_virtio.h"
38
39 static struct mic_driver *g_drv;
40
41 static int __init mic_dp_init(void)
42 {
43         struct mic_driver *mdrv = g_drv;
44         struct mic_device *mdev = &mdrv->mdev;
45         struct mic_bootparam __iomem *bootparam;
46         u64 lo, hi, dp_dma_addr;
47         u32 magic;
48
49         lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
50         hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
51
52         dp_dma_addr = lo | (hi << 32);
53         mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
54         if (!mdrv->dp) {
55                 dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
56                 return -ENOMEM;
57         }
58         bootparam = mdrv->dp;
59         magic = ioread32(&bootparam->magic);
60         if (MIC_MAGIC != magic) {
61                 dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
62                 return -EIO;
63         }
64         return 0;
65 }
66
67 /* Uninitialize the device page */
68 static void mic_dp_uninit(void)
69 {
70         mic_card_unmap(&g_drv->mdev, g_drv->dp);
71 }
72
73 /**
74  * mic_request_card_irq - request an irq.
75  *
76  * @handler: interrupt handler passed to request_threaded_irq.
77  * @thread_fn: thread fn. passed to request_threaded_irq.
78  * @name: The ASCII name of the callee requesting the irq.
79  * @data: private data that is returned back when calling the
80  * function handler.
81  * @index: The doorbell index of the requester.
82  *
83  * returns: The cookie that is transparent to the caller. Passed
84  * back when calling mic_free_irq. An appropriate error code
85  * is returned on failure. Caller needs to use IS_ERR(return_val)
86  * to check for failure and PTR_ERR(return_val) to obtained the
87  * error code.
88  *
89  */
90 struct mic_irq *
91 mic_request_card_irq(irq_handler_t handler,
92                      irq_handler_t thread_fn, const char *name,
93                      void *data, int index)
94 {
95         int rc = 0;
96         unsigned long cookie;
97         struct mic_driver *mdrv = g_drv;
98
99         rc  = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
100                                    thread_fn, 0, name, data);
101         if (rc) {
102                 dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
103                 goto err;
104         }
105         mdrv->irq_info.irq_usage_count[index]++;
106         cookie = index;
107         return (struct mic_irq *)cookie;
108 err:
109         return ERR_PTR(rc);
110 }
111
112 /**
113  * mic_free_card_irq - free irq.
114  *
115  * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
116  * @data: private data specified by the calling function during the
117  * mic_request_threaded_irq
118  *
119  * returns: none.
120  */
121 void mic_free_card_irq(struct mic_irq *cookie, void *data)
122 {
123         int index;
124         struct mic_driver *mdrv = g_drv;
125
126         index = (unsigned long)cookie & 0xFFFFU;
127         free_irq(mic_db_to_irq(mdrv, index), data);
128         mdrv->irq_info.irq_usage_count[index]--;
129 }
130
131 /**
132  * mic_next_card_db - Get the doorbell with minimum usage count.
133  *
134  * Returns the irq index.
135  */
136 int mic_next_card_db(void)
137 {
138         int i;
139         int index = 0;
140         struct mic_driver *mdrv = g_drv;
141
142         for (i = 0; i < mdrv->intr_info.num_intr; i++) {
143                 if (mdrv->irq_info.irq_usage_count[i] <
144                         mdrv->irq_info.irq_usage_count[index])
145                         index = i;
146         }
147
148         return index;
149 }
150
151 /**
152  * mic_init_irq - Initialize irq information.
153  *
154  * Returns 0 in success. Appropriate error code on failure.
155  */
156 static int mic_init_irq(void)
157 {
158         struct mic_driver *mdrv = g_drv;
159
160         mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
161                         mdrv->intr_info.num_intr),
162                         GFP_KERNEL);
163         if (!mdrv->irq_info.irq_usage_count)
164                 return -ENOMEM;
165         return 0;
166 }
167
168 /**
169  * mic_uninit_irq - Uninitialize irq information.
170  *
171  * None.
172  */
173 static void mic_uninit_irq(void)
174 {
175         struct mic_driver *mdrv = g_drv;
176
177         kfree(mdrv->irq_info.irq_usage_count);
178 }
179
180 static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
181 {
182         return dev_get_drvdata(scdev->dev.parent);
183 }
184
185 static struct mic_irq *
186 ___mic_request_irq(struct scif_hw_dev *scdev,
187                    irqreturn_t (*func)(int irq, void *data),
188                                        const char *name, void *data,
189                                        int db)
190 {
191         return mic_request_card_irq(func, NULL, name, data, db);
192 }
193
194 static void
195 ___mic_free_irq(struct scif_hw_dev *scdev,
196                 struct mic_irq *cookie, void *data)
197 {
198         return mic_free_card_irq(cookie, data);
199 }
200
201 static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
202 {
203         struct mic_driver *mdrv = scdev_to_mdrv(scdev);
204
205         mic_ack_interrupt(&mdrv->mdev);
206 }
207
208 static int ___mic_next_db(struct scif_hw_dev *scdev)
209 {
210         return mic_next_card_db();
211 }
212
213 static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
214 {
215         struct mic_driver *mdrv = scdev_to_mdrv(scdev);
216
217         mic_send_intr(&mdrv->mdev, db);
218 }
219
220 static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
221                                  struct mic_mw *mw)
222 {
223         mic_send_p2p_intr(db, mw);
224 }
225
226 static void __iomem *
227 ___mic_ioremap(struct scif_hw_dev *scdev,
228                phys_addr_t pa, size_t len)
229 {
230         struct mic_driver *mdrv = scdev_to_mdrv(scdev);
231
232         return mic_card_map(&mdrv->mdev, pa, len);
233 }
234
235 static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
236 {
237         struct mic_driver *mdrv = scdev_to_mdrv(scdev);
238
239         mic_card_unmap(&mdrv->mdev, va);
240 }
241
242 static struct scif_hw_ops scif_hw_ops = {
243         .request_irq = ___mic_request_irq,
244         .free_irq = ___mic_free_irq,
245         .ack_interrupt = ___mic_ack_interrupt,
246         .next_db = ___mic_next_db,
247         .send_intr = ___mic_send_intr,
248         .send_p2p_intr = ___mic_send_p2p_intr,
249         .ioremap = ___mic_ioremap,
250         .iounmap = ___mic_iounmap,
251 };
252
253 static int mic_request_dma_chans(struct mic_driver *mdrv)
254 {
255         dma_cap_mask_t mask;
256         struct dma_chan *chan;
257
258         request_module("mic_x100_dma");
259         dma_cap_zero(mask);
260         dma_cap_set(DMA_MEMCPY, mask);
261
262         do {
263                 chan = dma_request_channel(mask, NULL, NULL);
264                 if (chan) {
265                         mdrv->dma_ch[mdrv->num_dma_ch++] = chan;
266                         if (mdrv->num_dma_ch >= MIC_MAX_DMA_CHAN)
267                                 break;
268                 }
269         } while (chan);
270         dev_info(mdrv->dev, "DMA channels # %d\n", mdrv->num_dma_ch);
271         return mdrv->num_dma_ch;
272 }
273
274 static void mic_free_dma_chans(struct mic_driver *mdrv)
275 {
276         int i = 0;
277
278         for (i = 0; i < mdrv->num_dma_ch; i++) {
279                 dma_release_channel(mdrv->dma_ch[i]);
280                 mdrv->dma_ch[i] = NULL;
281         }
282         mdrv->num_dma_ch = 0;
283 }
284
285 /*
286  * mic_driver_init - MIC driver initialization tasks.
287  *
288  * Returns 0 in success. Appropriate error code on failure.
289  */
290 int __init mic_driver_init(struct mic_driver *mdrv)
291 {
292         int rc;
293         struct mic_bootparam __iomem *bootparam;
294         u8 node_id;
295
296         g_drv = mdrv;
297         /* Unloading the card module is not supported. */
298         if (!try_module_get(mdrv->dev->driver->owner)) {
299                 rc = -ENODEV;
300                 goto done;
301         }
302         rc = mic_dp_init();
303         if (rc)
304                 goto put;
305         rc = mic_init_irq();
306         if (rc)
307                 goto dp_uninit;
308         if (!mic_request_dma_chans(mdrv)) {
309                 rc = -ENODEV;
310                 goto irq_uninit;
311         }
312         rc = mic_devices_init(mdrv);
313         if (rc)
314                 goto dma_free;
315         bootparam = mdrv->dp;
316         node_id = ioread8(&bootparam->node_id);
317         mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
318                                            NULL, &scif_hw_ops,
319                                            0, node_id, &mdrv->mdev.mmio, NULL,
320                                            NULL, mdrv->dp, mdrv->dma_ch,
321                                            mdrv->num_dma_ch, true);
322         if (IS_ERR(mdrv->scdev)) {
323                 rc = PTR_ERR(mdrv->scdev);
324                 goto device_uninit;
325         }
326         mic_create_card_debug_dir(mdrv);
327 done:
328         return rc;
329 device_uninit:
330         mic_devices_uninit(mdrv);
331 dma_free:
332         mic_free_dma_chans(mdrv);
333 irq_uninit:
334         mic_uninit_irq();
335 dp_uninit:
336         mic_dp_uninit();
337 put:
338         module_put(mdrv->dev->driver->owner);
339         return rc;
340 }
341
342 /*
343  * mic_driver_uninit - MIC driver uninitialization tasks.
344  *
345  * Returns None
346  */
347 void mic_driver_uninit(struct mic_driver *mdrv)
348 {
349         mic_delete_card_debug_dir(mdrv);
350         scif_unregister_device(mdrv->scdev);
351         mic_devices_uninit(mdrv);
352         mic_free_dma_chans(mdrv);
353         mic_uninit_irq();
354         mic_dp_uninit();
355         module_put(mdrv->dev->driver->owner);
356 }