blob: 4007adc666f377d0ef4ddbf4f8bbdcb773abfa39 [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/platform_device.h>
30
Sudeep Dutt4aa79962013-09-27 09:49:42 -070031#include "../common/mic_dev.h"
Sudeep Duttaa27bad2013-09-05 16:42:06 -070032#include "mic_device.h"
33#include "mic_x100.h"
34
35static const char mic_driver_name[] = "mic";
36
37static struct mic_driver g_drv;
38
39/**
40 * mic_read_spad - read from the scratchpad register
41 * @mdev: pointer to mic_device instance
42 * @idx: index to scratchpad register, 0 based
43 *
44 * This function allows reading of the 32bit scratchpad register.
45 *
46 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
47 */
48u32 mic_read_spad(struct mic_device *mdev, unsigned int idx)
49{
50 return mic_mmio_read(&mdev->mmio,
51 MIC_X100_SBOX_BASE_ADDRESS +
52 MIC_X100_SBOX_SPAD0 + idx * 4);
53}
54
55/**
56 * __mic_send_intr - Send interrupt to Host.
57 * @mdev: pointer to mic_device instance
58 * @doorbell: Doorbell number.
59 */
60void mic_send_intr(struct mic_device *mdev, int doorbell)
61{
62 struct mic_mw *mw = &mdev->mmio;
63
64 if (doorbell > MIC_X100_MAX_DOORBELL_IDX)
65 return;
66 /* Ensure that the interrupt is ordered w.r.t previous stores. */
67 wmb();
68 mic_mmio_write(mw, MIC_X100_SBOX_SDBIC0_DBREQ_BIT,
Ashutosh Dixitced2c602013-09-27 09:49:53 -070069 MIC_X100_SBOX_BASE_ADDRESS +
70 (MIC_X100_SBOX_SDBIC0 + (4 * doorbell)));
Sudeep Duttaa27bad2013-09-05 16:42:06 -070071}
72
Sudeep Duttdd8d8d42015-04-29 05:32:39 -070073/*
74 * mic_x100_send_sbox_intr - Send an MIC_X100_SBOX interrupt to MIC.
75 */
76static void mic_x100_send_sbox_intr(struct mic_mw *mw, int doorbell)
77{
78 u64 apic_icr_offset = MIC_X100_SBOX_APICICR0 + doorbell * 8;
79 u32 apicicr_low = mic_mmio_read(mw, MIC_X100_SBOX_BASE_ADDRESS +
80 apic_icr_offset);
81
82 /* for MIC we need to make sure we "hit" the send_icr bit (13) */
83 apicicr_low = (apicicr_low | (1 << 13));
84 /*
85 * Ensure that the interrupt is ordered w.r.t. previous stores
86 * to main memory. Fence instructions are not implemented in X100
87 * since execution is in order but a compiler barrier is still
88 * required.
89 */
90 wmb();
91 mic_mmio_write(mw, apicicr_low,
92 MIC_X100_SBOX_BASE_ADDRESS + apic_icr_offset);
93}
94
95static void mic_x100_send_rdmasr_intr(struct mic_mw *mw, int doorbell)
96{
97 int rdmasr_offset = MIC_X100_SBOX_RDMASR0 + (doorbell << 2);
98 /*
99 * Ensure that the interrupt is ordered w.r.t. previous stores
100 * to main memory. Fence instructions are not implemented in X100
101 * since execution is in order but a compiler barrier is still
102 * required.
103 */
104 wmb();
105 mic_mmio_write(mw, 0, MIC_X100_SBOX_BASE_ADDRESS + rdmasr_offset);
106}
107
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700108/**
109 * mic_ack_interrupt - Device specific interrupt handling.
110 * @mdev: pointer to mic_device instance
111 *
112 * Returns: bitmask of doorbell events triggered.
113 */
114u32 mic_ack_interrupt(struct mic_device *mdev)
115{
116 return 0;
117}
118
119static inline int mic_get_sbox_irq(int db)
120{
121 return MIC_X100_IRQ_BASE + db;
122}
123
124static inline int mic_get_rdmasr_irq(int index)
125{
126 return MIC_X100_RDMASR_IRQ_BASE + index;
127}
128
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700129void mic_send_p2p_intr(int db, struct mic_mw *mw)
130{
131 int rdmasr_index;
132
133 if (db < MIC_X100_NUM_SBOX_IRQ) {
134 mic_x100_send_sbox_intr(mw, db);
135 } else {
136 rdmasr_index = db - MIC_X100_NUM_SBOX_IRQ;
137 mic_x100_send_rdmasr_intr(mw, rdmasr_index);
138 }
139}
140
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700141/**
142 * mic_hw_intr_init - Initialize h/w specific interrupt
143 * information.
144 * @mdrv: pointer to mic_driver
145 */
146void mic_hw_intr_init(struct mic_driver *mdrv)
147{
148 mdrv->intr_info.num_intr = MIC_X100_NUM_SBOX_IRQ +
149 MIC_X100_NUM_RDMASR_IRQ;
150}
151
152/**
153 * mic_db_to_irq - Retrieve irq number corresponding to a doorbell.
154 * @mdrv: pointer to mic_driver
155 * @db: The doorbell obtained for which the irq is needed. Doorbell
156 * may correspond to an sbox doorbell or an rdmasr index.
157 *
158 * Returns the irq corresponding to the doorbell.
159 */
160int mic_db_to_irq(struct mic_driver *mdrv, int db)
161{
162 int rdmasr_index;
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700163
164 /*
165 * The total number of doorbell interrupts on the card are 16. Indices
166 * 0-8 falls in the SBOX category and 8-15 fall in the RDMASR category.
167 */
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700168 if (db < MIC_X100_NUM_SBOX_IRQ) {
169 return mic_get_sbox_irq(db);
170 } else {
Sudeep Duttdd8d8d42015-04-29 05:32:39 -0700171 rdmasr_index = db - MIC_X100_NUM_SBOX_IRQ;
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700172 return mic_get_rdmasr_irq(rdmasr_index);
173 }
174}
175
176/*
177 * mic_card_map - Allocate virtual address for a remote memory region.
178 * @mdev: pointer to mic_device instance.
179 * @addr: Remote DMA address.
180 * @size: Size of the region.
181 *
182 * Returns: Virtual address backing the remote memory region.
183 */
184void __iomem *
185mic_card_map(struct mic_device *mdev, dma_addr_t addr, size_t size)
186{
187 return ioremap(addr, size);
188}
189
190/*
191 * mic_card_unmap - Unmap the virtual address for a remote memory region.
192 * @mdev: pointer to mic_device instance.
193 * @addr: Virtual address for remote memory region.
194 *
195 * Returns: None.
196 */
197void mic_card_unmap(struct mic_device *mdev, void __iomem *addr)
198{
199 iounmap(addr);
200}
201
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700202static inline struct mic_driver *mbdev_to_mdrv(struct mbus_device *mbdev)
203{
204 return dev_get_drvdata(mbdev->dev.parent);
205}
206
207static struct mic_irq *
208_mic_request_threaded_irq(struct mbus_device *mbdev,
209 irq_handler_t handler, irq_handler_t thread_fn,
210 const char *name, void *data, int intr_src)
211{
212 int rc = 0;
213 unsigned int irq = intr_src;
214 unsigned long cookie = irq;
215
216 rc = request_threaded_irq(irq, handler, thread_fn, 0, name, data);
217 if (rc) {
218 dev_err(mbdev_to_mdrv(mbdev)->dev,
219 "request_threaded_irq failed rc = %d\n", rc);
220 return ERR_PTR(rc);
221 }
222 return (struct mic_irq *)cookie;
223}
224
225static void _mic_free_irq(struct mbus_device *mbdev,
226 struct mic_irq *cookie, void *data)
227{
228 unsigned long irq = (unsigned long)cookie;
229 free_irq(irq, data);
230}
231
232static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
233{
234 mic_ack_interrupt(&mbdev_to_mdrv(mbdev)->mdev);
235}
236
237static struct mbus_hw_ops mbus_hw_ops = {
238 .request_threaded_irq = _mic_request_threaded_irq,
239 .free_irq = _mic_free_irq,
240 .ack_interrupt = _mic_ack_interrupt,
241};
242
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700243static int __init mic_probe(struct platform_device *pdev)
244{
245 struct mic_driver *mdrv = &g_drv;
246 struct mic_device *mdev = &mdrv->mdev;
247 int rc = 0;
248
249 mdrv->dev = &pdev->dev;
250 snprintf(mdrv->name, sizeof(mic_driver_name), mic_driver_name);
251
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200252 /* FIXME: use dma_set_mask_and_coherent() and check result */
253 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
254
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700255 mdev->mmio.pa = MIC_X100_MMIO_BASE;
256 mdev->mmio.len = MIC_X100_MMIO_LEN;
Himangi Saraogie1ada0f2014-07-14 14:23:51 -0700257 mdev->mmio.va = devm_ioremap(&pdev->dev, MIC_X100_MMIO_BASE,
258 MIC_X100_MMIO_LEN);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700259 if (!mdev->mmio.va) {
260 dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
261 rc = -EIO;
262 goto done;
263 }
264 mic_hw_intr_init(mdrv);
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700265 platform_set_drvdata(pdev, mdrv);
266 mdrv->dma_mbdev = mbus_register_device(mdrv->dev, MBUS_DEV_DMA_MIC,
Ashutosh Dixitd411e792015-09-29 18:13:54 -0700267 NULL, &mbus_hw_ops, 0,
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700268 mdrv->mdev.mmio.va);
269 if (IS_ERR(mdrv->dma_mbdev)) {
270 rc = PTR_ERR(mdrv->dma_mbdev);
271 dev_err(&pdev->dev, "mbus_add_device failed rc %d\n", rc);
Himangi Saraogie1ada0f2014-07-14 14:23:51 -0700272 goto done;
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700273 }
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700274 rc = mic_driver_init(mdrv);
275 if (rc) {
276 dev_err(&pdev->dev, "mic_driver_init failed rc %d\n", rc);
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700277 goto remove_dma;
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700278 }
279done:
280 return rc;
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700281remove_dma:
282 mbus_unregister_device(mdrv->dma_mbdev);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700283 return rc;
284}
285
286static int mic_remove(struct platform_device *pdev)
287{
288 struct mic_driver *mdrv = &g_drv;
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700289
290 mic_driver_uninit(mdrv);
Siva Yerramreddya93a5242014-07-11 14:04:25 -0700291 mbus_unregister_device(mdrv->dma_mbdev);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700292 return 0;
293}
294
295static void mic_platform_shutdown(struct platform_device *pdev)
296{
297 mic_remove(pdev);
298}
299
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700300static struct platform_driver __refdata mic_platform_driver = {
301 .probe = mic_probe,
302 .remove = mic_remove,
303 .shutdown = mic_platform_shutdown,
304 .driver = {
305 .name = mic_driver_name,
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700306 },
307};
308
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200309static struct platform_device *mic_platform_dev;
310
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700311static int __init mic_init(void)
312{
313 int ret;
314 struct cpuinfo_x86 *c = &cpu_data(0);
315
316 if (!(c->x86 == 11 && c->x86_model == 1)) {
317 ret = -ENODEV;
318 pr_err("%s not running on X100 ret %d\n", __func__, ret);
319 goto done;
320 }
321
Sudeep Duttc74c9312016-02-08 15:48:18 -0800322 request_module("mic_x100_dma");
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700323 mic_init_card_debugfs();
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200324
325 mic_platform_dev = platform_device_register_simple(mic_driver_name,
326 0, NULL, 0);
327 ret = PTR_ERR_OR_ZERO(mic_platform_dev);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700328 if (ret) {
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200329 pr_err("platform_device_register_full ret %d\n", ret);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700330 goto cleanup_debugfs;
331 }
332 ret = platform_driver_register(&mic_platform_driver);
333 if (ret) {
334 pr_err("platform_driver_register ret %d\n", ret);
335 goto device_unregister;
336 }
337 return ret;
338
339device_unregister:
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200340 platform_device_unregister(mic_platform_dev);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700341cleanup_debugfs:
342 mic_exit_card_debugfs();
343done:
344 return ret;
345}
346
347static void __exit mic_exit(void)
348{
349 platform_driver_unregister(&mic_platform_driver);
Arnd Bergmann7ed623c2019-07-12 11:24:09 +0200350 platform_device_unregister(mic_platform_dev);
Sudeep Duttaa27bad2013-09-05 16:42:06 -0700351 mic_exit_card_debugfs();
352}
353
354module_init(mic_init);
355module_exit(mic_exit);
356
357MODULE_AUTHOR("Intel Corporation");
358MODULE_DESCRIPTION("Intel(R) MIC X100 Card driver");
359MODULE_LICENSE("GPL v2");