blob: 6338908b225228a0a77ff08ce68572363fa8a760 [file] [log] [blame]
Sudeep Duttaa27bad2013-09-05 16:42:06 -07001/*
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>
Sudeep Duttdd8d8d42015-04-29 05:32:39 -070031#include <linux/dmaengine.h>
32#include <linux/kmod.h>
Sudeep Duttaa27bad2013-09-05 16:42:06 -070033
34#include <linux/mic_common.h>
Sudeep Dutt4aa79962013-09-27 09:49:42 -070035#include "../common/mic_dev.h"
Sudeep Duttaa27bad2013-09-05 16:42:06 -070036#include "mic_device.h"
Ashutosh Dixit2141c7c2013-09-05 16:42:28 -070037#include "mic_virtio.h"
Sudeep Duttaa27bad2013-09-05 16:42:06 -070038
39static struct mic_driver *g_drv;
40static struct mic_irq *shutdown_cookie;
41
42static void mic_notify_host(u8 state)
43{
44 struct mic_driver *mdrv = g_drv;
45 struct mic_bootparam __iomem *bootparam = mdrv->dp;
46
47 iowrite8(state, &bootparam->shutdown_status);
48 dev_dbg(mdrv->dev, "%s %d system_state %d\n",
49 __func__, __LINE__, state);
50 mic_send_intr(&mdrv->mdev, ioread8(&bootparam->c2h_shutdown_db));
51}
52
53static int mic_panic_event(struct notifier_block *this, unsigned long event,
54 void *ptr)
55{
56 struct mic_driver *mdrv = g_drv;
57 struct mic_bootparam __iomem *bootparam = mdrv->dp;
58
59 iowrite8(-1, &bootparam->h2c_config_db);
60 iowrite8(-1, &bootparam->h2c_shutdown_db);
61 mic_notify_host(MIC_CRASHED);
62 return NOTIFY_DONE;
63}
64
65static struct notifier_block mic_panic = {
66 .notifier_call = mic_panic_event,
67};
68
69static irqreturn_t mic_shutdown_isr(int irq, void *data)
70{
71 struct mic_driver *mdrv = g_drv;
72 struct mic_bootparam __iomem *bootparam = mdrv->dp;
73
74 mic_ack_interrupt(&g_drv->mdev);
75 if (ioread8(&bootparam->shutdown_card))
76 orderly_poweroff(true);
77 return IRQ_HANDLED;
78}
79
80static int mic_shutdown_init(void)
81{
82 int rc = 0;
83 struct mic_driver *mdrv = g_drv;
84 struct mic_bootparam __iomem *bootparam = mdrv->dp;
85 int shutdown_db;
86
87 shutdown_db = mic_next_card_db();
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -070088 shutdown_cookie = mic_request_card_irq(mic_shutdown_isr, NULL,
89 "Shutdown", mdrv, shutdown_db);
Sudeep Duttaa27bad2013-09-05 16:42:06 -070090 if (IS_ERR(shutdown_cookie))
91 rc = PTR_ERR(shutdown_cookie);
92 else
93 iowrite8(shutdown_db, &bootparam->h2c_shutdown_db);
94 return rc;
95}
96
97static void mic_shutdown_uninit(void)
98{
99 struct mic_driver *mdrv = g_drv;
100 struct mic_bootparam __iomem *bootparam = mdrv->dp;
101
102 iowrite8(-1, &bootparam->h2c_shutdown_db);
103 mic_free_card_irq(shutdown_cookie, mdrv);
104}
105
106static int __init mic_dp_init(void)
107{
108 struct mic_driver *mdrv = g_drv;
109 struct mic_device *mdev = &mdrv->mdev;
110 struct mic_bootparam __iomem *bootparam;
111 u64 lo, hi, dp_dma_addr;
112 u32 magic;
113
114 lo = mic_read_spad(&mdrv->mdev, MIC_DPLO_SPAD);
115 hi = mic_read_spad(&mdrv->mdev, MIC_DPHI_SPAD);
116
117 dp_dma_addr = lo | (hi << 32);
118 mdrv->dp = mic_card_map(mdev, dp_dma_addr, MIC_DP_SIZE);
119 if (!mdrv->dp) {
120 dev_err(mdrv->dev, "Cannot remap Aperture BAR\n");
121 return -ENOMEM;
122 }
123 bootparam = mdrv->dp;
124 magic = ioread32(&bootparam->magic);
125 if (MIC_MAGIC != magic) {
126 dev_err(mdrv->dev, "bootparam magic mismatch 0x%x\n", magic);
127 return -EIO;
128 }
129 return 0;
130}
131
132/* Uninitialize the device page */
133static void mic_dp_uninit(void)
134{
135 mic_card_unmap(&g_drv->mdev, g_drv->dp);
136}
137
138/**
139 * mic_request_card_irq - request an irq.
140 *
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700141 * @handler: interrupt handler passed to request_threaded_irq.
142 * @thread_fn: thread fn. passed to request_threaded_irq.
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700143 * @name: The ASCII name of the callee requesting the irq.
144 * @data: private data that is returned back when calling the
145 * function handler.
146 * @index: The doorbell index of the requester.
147 *
148 * returns: The cookie that is transparent to the caller. Passed
149 * back when calling mic_free_irq. An appropriate error code
150 * is returned on failure. Caller needs to use IS_ERR(return_val)
151 * to check for failure and PTR_ERR(return_val) to obtained the
152 * error code.
153 *
154 */
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700155struct mic_irq *
156mic_request_card_irq(irq_handler_t handler,
157 irq_handler_t thread_fn, const char *name,
158 void *data, int index)
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700159{
160 int rc = 0;
161 unsigned long cookie;
162 struct mic_driver *mdrv = g_drv;
163
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700164 rc = request_threaded_irq(mic_db_to_irq(mdrv, index), handler,
165 thread_fn, 0, name, data);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700166 if (rc) {
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700167 dev_err(mdrv->dev, "request_threaded_irq failed rc = %d\n", rc);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700168 goto err;
169 }
170 mdrv->irq_info.irq_usage_count[index]++;
171 cookie = index;
172 return (struct mic_irq *)cookie;
173err:
174 return ERR_PTR(rc);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700175}
176
177/**
178 * mic_free_card_irq - free irq.
179 *
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700180 * @cookie: cookie obtained during a successful call to mic_request_threaded_irq
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700181 * @data: private data specified by the calling function during the
Siva Yerramreddy9c3d37c2014-07-11 14:04:24 -0700182 * mic_request_threaded_irq
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700183 *
184 * returns: none.
185 */
186void mic_free_card_irq(struct mic_irq *cookie, void *data)
187{
188 int index;
189 struct mic_driver *mdrv = g_drv;
190
191 index = (unsigned long)cookie & 0xFFFFU;
192 free_irq(mic_db_to_irq(mdrv, index), data);
193 mdrv->irq_info.irq_usage_count[index]--;
194}
195
196/**
197 * mic_next_card_db - Get the doorbell with minimum usage count.
198 *
199 * Returns the irq index.
200 */
201int mic_next_card_db(void)
202{
203 int i;
204 int index = 0;
205 struct mic_driver *mdrv = g_drv;
206
207 for (i = 0; i < mdrv->intr_info.num_intr; i++) {
208 if (mdrv->irq_info.irq_usage_count[i] <
209 mdrv->irq_info.irq_usage_count[index])
210 index = i;
211 }
212
213 return index;
214}
215
216/**
217 * mic_init_irq - Initialize irq information.
218 *
219 * Returns 0 in success. Appropriate error code on failure.
220 */
221static int mic_init_irq(void)
222{
223 struct mic_driver *mdrv = g_drv;
224
225 mdrv->irq_info.irq_usage_count = kzalloc((sizeof(u32) *
226 mdrv->intr_info.num_intr),
227 GFP_KERNEL);
228 if (!mdrv->irq_info.irq_usage_count)
229 return -ENOMEM;
230 return 0;
231}
232
233/**
234 * mic_uninit_irq - Uninitialize irq information.
235 *
236 * None.
237 */
238static void mic_uninit_irq(void)
239{
240 struct mic_driver *mdrv = g_drv;
241
242 kfree(mdrv->irq_info.irq_usage_count);
243}
244
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700245static inline struct mic_driver *scdev_to_mdrv(struct scif_hw_dev *scdev)
246{
247 return dev_get_drvdata(scdev->dev.parent);
248}
249
250static struct mic_irq *
251___mic_request_irq(struct scif_hw_dev *scdev,
252 irqreturn_t (*func)(int irq, void *data),
253 const char *name, void *data,
254 int db)
255{
256 return mic_request_card_irq(func, NULL, name, data, db);
257}
258
259static void
260___mic_free_irq(struct scif_hw_dev *scdev,
261 struct mic_irq *cookie, void *data)
262{
263 return mic_free_card_irq(cookie, data);
264}
265
266static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
267{
268 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
269
270 mic_ack_interrupt(&mdrv->mdev);
271}
272
273static int ___mic_next_db(struct scif_hw_dev *scdev)
274{
275 return mic_next_card_db();
276}
277
278static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
279{
280 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
281
282 mic_send_intr(&mdrv->mdev, db);
283}
284
285static void ___mic_send_p2p_intr(struct scif_hw_dev *scdev, int db,
286 struct mic_mw *mw)
287{
288 mic_send_p2p_intr(db, mw);
289}
290
291static void __iomem *
292___mic_ioremap(struct scif_hw_dev *scdev,
293 phys_addr_t pa, size_t len)
294{
295 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
296
297 return mic_card_map(&mdrv->mdev, pa, len);
298}
299
300static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
301{
302 struct mic_driver *mdrv = scdev_to_mdrv(scdev);
303
304 mic_card_unmap(&mdrv->mdev, va);
305}
306
307static struct scif_hw_ops scif_hw_ops = {
308 .request_irq = ___mic_request_irq,
309 .free_irq = ___mic_free_irq,
310 .ack_interrupt = ___mic_ack_interrupt,
311 .next_db = ___mic_next_db,
312 .send_intr = ___mic_send_intr,
313 .send_p2p_intr = ___mic_send_p2p_intr,
314 .ioremap = ___mic_ioremap,
315 .iounmap = ___mic_iounmap,
316};
317
318static int mic_request_dma_chans(struct mic_driver *mdrv)
319{
320 dma_cap_mask_t mask;
321 struct dma_chan *chan;
322
323 request_module("mic_x100_dma");
324 dma_cap_zero(mask);
325 dma_cap_set(DMA_MEMCPY, mask);
326
327 do {
328 chan = dma_request_channel(mask, NULL, NULL);
329 if (chan) {
330 mdrv->dma_ch[mdrv->num_dma_ch++] = chan;
331 if (mdrv->num_dma_ch >= MIC_MAX_DMA_CHAN)
332 break;
333 }
334 } while (chan);
335 dev_info(mdrv->dev, "DMA channels # %d\n", mdrv->num_dma_ch);
336 return mdrv->num_dma_ch;
337}
338
339static void mic_free_dma_chans(struct mic_driver *mdrv)
340{
341 int i = 0;
342
343 for (i = 0; i < mdrv->num_dma_ch; i++) {
344 dma_release_channel(mdrv->dma_ch[i]);
345 mdrv->dma_ch[i] = NULL;
346 }
347 mdrv->num_dma_ch = 0;
348}
349
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700350/*
351 * mic_driver_init - MIC driver initialization tasks.
352 *
353 * Returns 0 in success. Appropriate error code on failure.
354 */
355int __init mic_driver_init(struct mic_driver *mdrv)
356{
357 int rc;
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700358 struct mic_bootparam __iomem *bootparam;
359 u8 node_id;
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700360
361 g_drv = mdrv;
362 /*
363 * Unloading the card module is not supported. The MIC card module
364 * handles fundamental operations like host/card initiated shutdowns
365 * and informing the host about card crashes and cannot be unloaded.
366 */
367 if (!try_module_get(mdrv->dev->driver->owner)) {
368 rc = -ENODEV;
369 goto done;
370 }
371 rc = mic_dp_init();
372 if (rc)
373 goto put;
374 rc = mic_init_irq();
375 if (rc)
376 goto dp_uninit;
377 rc = mic_shutdown_init();
378 if (rc)
379 goto irq_uninit;
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700380 if (!mic_request_dma_chans(mdrv)) {
381 rc = -ENODEV;
382 goto shutdown_uninit;
383 }
Ashutosh Dixit2141c7c2013-09-05 16:42:28 -0700384 rc = mic_devices_init(mdrv);
385 if (rc)
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700386 goto dma_free;
387 bootparam = mdrv->dp;
388 node_id = ioread8(&bootparam->node_id);
389 mdrv->scdev = scif_register_device(mdrv->dev, MIC_SCIF_DEV,
390 NULL, &scif_hw_ops,
391 0, node_id, &mdrv->mdev.mmio, NULL,
392 NULL, mdrv->dp, mdrv->dma_ch,
393 mdrv->num_dma_ch);
394 if (IS_ERR(mdrv->scdev)) {
395 rc = PTR_ERR(mdrv->scdev);
396 goto device_uninit;
397 }
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700398 mic_create_card_debug_dir(mdrv);
399 atomic_notifier_chain_register(&panic_notifier_list, &mic_panic);
400done:
401 return rc;
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700402device_uninit:
403 mic_devices_uninit(mdrv);
404dma_free:
405 mic_free_dma_chans(mdrv);
Ashutosh Dixit2141c7c2013-09-05 16:42:28 -0700406shutdown_uninit:
407 mic_shutdown_uninit();
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700408irq_uninit:
409 mic_uninit_irq();
410dp_uninit:
411 mic_dp_uninit();
412put:
413 module_put(mdrv->dev->driver->owner);
414 return rc;
415}
416
417/*
418 * mic_driver_uninit - MIC driver uninitialization tasks.
419 *
420 * Returns None
421 */
422void mic_driver_uninit(struct mic_driver *mdrv)
423{
424 mic_delete_card_debug_dir(mdrv);
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700425 scif_unregister_device(mdrv->scdev);
Ashutosh Dixit2141c7c2013-09-05 16:42:28 -0700426 mic_devices_uninit(mdrv);
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700427 mic_free_dma_chans(mdrv);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700428 /*
429 * Inform the host about the shutdown status i.e. poweroff/restart etc.
430 * The module cannot be unloaded so the only code path to call
431 * mic_devices_uninit(..) is the shutdown callback.
432 */
433 mic_notify_host(system_state);
434 mic_shutdown_uninit();
435 mic_uninit_irq();
436 mic_dp_uninit();
437 module_put(mdrv->dev->driver->owner);
438}