Merge tag 'for-linus-5.9-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / misc / mic / host / mic_x100.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2013 Intel Corporation.
6  *
7  * Intel MIC Host driver.
8  */
9 #include <linux/fs.h>
10 #include <linux/pci.h>
11 #include <linux/sched.h>
12 #include <linux/firmware.h>
13 #include <linux/delay.h>
14
15 #include "../common/mic_dev.h"
16 #include "mic_device.h"
17 #include "mic_x100.h"
18 #include "mic_smpt.h"
19
20 static const u16 mic_x100_intr_init[] = {
21                 MIC_X100_DOORBELL_IDX_START,
22                 MIC_X100_DMA_IDX_START,
23                 MIC_X100_ERR_IDX_START,
24                 MIC_X100_NUM_DOORBELL,
25                 MIC_X100_NUM_DMA,
26                 MIC_X100_NUM_ERR,
27 };
28
29 /**
30  * mic_x100_write_spad - write to the scratchpad register
31  * @mdev: pointer to mic_device instance
32  * @idx: index to the scratchpad register, 0 based
33  * @val: the data value to put into the register
34  *
35  * This function allows writing of a 32bit value to the indexed scratchpad
36  * register.
37  *
38  * RETURNS: none.
39  */
40 static void
41 mic_x100_write_spad(struct mic_device *mdev, unsigned int idx, u32 val)
42 {
43         dev_dbg(&mdev->pdev->dev, "Writing 0x%x to scratch pad index %d\n",
44                 val, idx);
45         mic_mmio_write(&mdev->mmio, val,
46                        MIC_X100_SBOX_BASE_ADDRESS +
47                        MIC_X100_SBOX_SPAD0 + idx * 4);
48 }
49
50 /**
51  * mic_x100_read_spad - read from the scratchpad register
52  * @mdev: pointer to mic_device instance
53  * @idx: index to scratchpad register, 0 based
54  *
55  * This function allows reading of the 32bit scratchpad register.
56  *
57  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
58  */
59 static u32
60 mic_x100_read_spad(struct mic_device *mdev, unsigned int idx)
61 {
62         u32 val = mic_mmio_read(&mdev->mmio,
63                 MIC_X100_SBOX_BASE_ADDRESS +
64                 MIC_X100_SBOX_SPAD0 + idx * 4);
65
66         dev_dbg(&mdev->pdev->dev,
67                 "Reading 0x%x from scratch pad index %d\n", val, idx);
68         return val;
69 }
70
71 /**
72  * mic_x100_enable_interrupts - Enable interrupts.
73  * @mdev: pointer to mic_device instance
74  */
75 static void mic_x100_enable_interrupts(struct mic_device *mdev)
76 {
77         u32 reg;
78         struct mic_mw *mw = &mdev->mmio;
79         u32 sice0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SICE0;
80         u32 siac0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SIAC0;
81
82         reg = mic_mmio_read(mw, sice0);
83         reg |= MIC_X100_SBOX_DBR_BITS(0xf) | MIC_X100_SBOX_DMA_BITS(0xff);
84         mic_mmio_write(mw, reg, sice0);
85
86         /*
87          * Enable auto-clear when enabling interrupts. Applicable only for
88          * MSI-x. Legacy and MSI mode cannot have auto-clear enabled.
89          */
90         if (mdev->irq_info.num_vectors > 1) {
91                 reg = mic_mmio_read(mw, siac0);
92                 reg |= MIC_X100_SBOX_DBR_BITS(0xf) |
93                         MIC_X100_SBOX_DMA_BITS(0xff);
94                 mic_mmio_write(mw, reg, siac0);
95         }
96 }
97
98 /**
99  * mic_x100_disable_interrupts - Disable interrupts.
100  * @mdev: pointer to mic_device instance
101  */
102 static void mic_x100_disable_interrupts(struct mic_device *mdev)
103 {
104         u32 reg;
105         struct mic_mw *mw = &mdev->mmio;
106         u32 sice0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SICE0;
107         u32 siac0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SIAC0;
108         u32 sicc0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SICC0;
109
110         reg = mic_mmio_read(mw, sice0);
111         mic_mmio_write(mw, reg, sicc0);
112
113         if (mdev->irq_info.num_vectors > 1) {
114                 reg = mic_mmio_read(mw, siac0);
115                 reg &= ~(MIC_X100_SBOX_DBR_BITS(0xf) |
116                         MIC_X100_SBOX_DMA_BITS(0xff));
117                 mic_mmio_write(mw, reg, siac0);
118         }
119 }
120
121 /**
122  * mic_x100_send_sbox_intr - Send an MIC_X100_SBOX interrupt to MIC.
123  * @mdev: pointer to mic_device instance
124  * @doorbell: doorbell number
125  */
126 static void mic_x100_send_sbox_intr(struct mic_device *mdev,
127                                     int doorbell)
128 {
129         struct mic_mw *mw = &mdev->mmio;
130         u64 apic_icr_offset = MIC_X100_SBOX_APICICR0 + doorbell * 8;
131         u32 apicicr_low = mic_mmio_read(mw, MIC_X100_SBOX_BASE_ADDRESS +
132                                         apic_icr_offset);
133
134         /* for MIC we need to make sure we "hit" the send_icr bit (13) */
135         apicicr_low = (apicicr_low | (1 << 13));
136
137         /* Ensure that the interrupt is ordered w.r.t. previous stores. */
138         wmb();
139         mic_mmio_write(mw, apicicr_low,
140                        MIC_X100_SBOX_BASE_ADDRESS + apic_icr_offset);
141 }
142
143 /**
144  * mic_x100_send_rdmasr_intr - Send an RDMASR interrupt to MIC.
145  * @mdev: pointer to mic_device instance
146  * @doorbell: doorbell number
147  */
148 static void mic_x100_send_rdmasr_intr(struct mic_device *mdev,
149                                       int doorbell)
150 {
151         int rdmasr_offset = MIC_X100_SBOX_RDMASR0 + (doorbell << 2);
152         /* Ensure that the interrupt is ordered w.r.t. previous stores. */
153         wmb();
154         mic_mmio_write(&mdev->mmio, 0,
155                        MIC_X100_SBOX_BASE_ADDRESS + rdmasr_offset);
156 }
157
158 /**
159  * __mic_x100_send_intr - Send interrupt to MIC.
160  * @mdev: pointer to mic_device instance
161  * @doorbell: doorbell number.
162  */
163 static void mic_x100_send_intr(struct mic_device *mdev, int doorbell)
164 {
165         int rdmasr_db;
166         if (doorbell < MIC_X100_NUM_SBOX_IRQ) {
167                 mic_x100_send_sbox_intr(mdev, doorbell);
168         } else {
169                 rdmasr_db = doorbell - MIC_X100_NUM_SBOX_IRQ;
170                 mic_x100_send_rdmasr_intr(mdev, rdmasr_db);
171         }
172 }
173
174 /**
175  * mic_x100_ack_interrupt - Read the interrupt sources register and
176  * clear it. This function will be called in the MSI/INTx case.
177  * @mdev: Pointer to mic_device instance.
178  *
179  * Returns: bitmask of interrupt sources triggered.
180  */
181 static u32 mic_x100_ack_interrupt(struct mic_device *mdev)
182 {
183         u32 sicr0 = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_SICR0;
184         u32 reg = mic_mmio_read(&mdev->mmio, sicr0);
185         mic_mmio_write(&mdev->mmio, reg, sicr0);
186         return reg;
187 }
188
189 /**
190  * mic_x100_intr_workarounds - These hardware specific workarounds are
191  * to be invoked everytime an interrupt is handled.
192  * @mdev: Pointer to mic_device instance.
193  *
194  * Returns: none
195  */
196 static void mic_x100_intr_workarounds(struct mic_device *mdev)
197 {
198         struct mic_mw *mw = &mdev->mmio;
199
200         /* Clear pending bit array. */
201         if (MIC_A0_STEP == mdev->stepping)
202                 mic_mmio_write(mw, 1, MIC_X100_SBOX_BASE_ADDRESS +
203                         MIC_X100_SBOX_MSIXPBACR);
204
205         if (mdev->stepping >= MIC_B0_STEP)
206                 mdev->intr_ops->enable_interrupts(mdev);
207 }
208
209 /**
210  * mic_x100_hw_intr_init - Initialize h/w specific interrupt
211  * information.
212  * @mdev: pointer to mic_device instance
213  */
214 static void mic_x100_hw_intr_init(struct mic_device *mdev)
215 {
216         mdev->intr_info = (struct mic_intr_info *)mic_x100_intr_init;
217 }
218
219 /**
220  * mic_x100_read_msi_to_src_map - read from the MSI mapping registers
221  * @mdev: pointer to mic_device instance
222  * @idx: index to the mapping register, 0 based
223  *
224  * This function allows reading of the 32bit MSI mapping register.
225  *
226  * RETURNS: The value in the register.
227  */
228 static u32
229 mic_x100_read_msi_to_src_map(struct mic_device *mdev, int idx)
230 {
231         return mic_mmio_read(&mdev->mmio,
232                 MIC_X100_SBOX_BASE_ADDRESS +
233                 MIC_X100_SBOX_MXAR0 + idx * 4);
234 }
235
236 /**
237  * mic_x100_program_msi_to_src_map - program the MSI mapping registers
238  * @mdev: pointer to mic_device instance
239  * @idx: index to the mapping register, 0 based
240  * @offset: The bit offset in the register that needs to be updated.
241  * @set: boolean specifying if the bit in the specified offset needs
242  * to be set or cleared.
243  *
244  * RETURNS: None.
245  */
246 static void
247 mic_x100_program_msi_to_src_map(struct mic_device *mdev,
248                                 int idx, int offset, bool set)
249 {
250         unsigned long reg;
251         struct mic_mw *mw = &mdev->mmio;
252         u32 mxar = MIC_X100_SBOX_BASE_ADDRESS +
253                 MIC_X100_SBOX_MXAR0 + idx * 4;
254
255         reg = mic_mmio_read(mw, mxar);
256         if (set)
257                 __set_bit(offset, &reg);
258         else
259                 __clear_bit(offset, &reg);
260         mic_mmio_write(mw, reg, mxar);
261 }
262
263 /*
264  * mic_x100_reset_fw_ready - Reset Firmware ready status field.
265  * @mdev: pointer to mic_device instance
266  */
267 static void mic_x100_reset_fw_ready(struct mic_device *mdev)
268 {
269         mdev->ops->write_spad(mdev, MIC_X100_DOWNLOAD_INFO, 0);
270 }
271
272 /*
273  * mic_x100_is_fw_ready - Check if firmware is ready.
274  * @mdev: pointer to mic_device instance
275  */
276 static bool mic_x100_is_fw_ready(struct mic_device *mdev)
277 {
278         u32 scratch2 = mdev->ops->read_spad(mdev, MIC_X100_DOWNLOAD_INFO);
279         return MIC_X100_SPAD2_DOWNLOAD_STATUS(scratch2) ? true : false;
280 }
281
282 /**
283  * mic_x100_get_apic_id - Get bootstrap APIC ID.
284  * @mdev: pointer to mic_device instance
285  */
286 static u32 mic_x100_get_apic_id(struct mic_device *mdev)
287 {
288         u32 scratch2 = 0;
289
290         scratch2 = mdev->ops->read_spad(mdev, MIC_X100_DOWNLOAD_INFO);
291         return MIC_X100_SPAD2_APIC_ID(scratch2);
292 }
293
294 /**
295  * mic_x100_send_firmware_intr - Send an interrupt to the firmware on MIC.
296  * @mdev: pointer to mic_device instance
297  */
298 static void mic_x100_send_firmware_intr(struct mic_device *mdev)
299 {
300         u32 apicicr_low;
301         u64 apic_icr_offset = MIC_X100_SBOX_APICICR7;
302         int vector = MIC_X100_BSP_INTERRUPT_VECTOR;
303         struct mic_mw *mw = &mdev->mmio;
304
305         /*
306          * For MIC we need to make sure we "hit"
307          * the send_icr bit (13).
308          */
309         apicicr_low = (vector | (1 << 13));
310
311         mic_mmio_write(mw, mic_x100_get_apic_id(mdev),
312                        MIC_X100_SBOX_BASE_ADDRESS + apic_icr_offset + 4);
313
314         /* Ensure that the interrupt is ordered w.r.t. previous stores. */
315         wmb();
316         mic_mmio_write(mw, apicicr_low,
317                        MIC_X100_SBOX_BASE_ADDRESS + apic_icr_offset);
318 }
319
320 /**
321  * mic_x100_hw_reset - Reset the MIC device.
322  * @mdev: pointer to mic_device instance
323  */
324 static void mic_x100_hw_reset(struct mic_device *mdev)
325 {
326         u32 reset_reg;
327         u32 rgcr = MIC_X100_SBOX_BASE_ADDRESS + MIC_X100_SBOX_RGCR;
328         struct mic_mw *mw = &mdev->mmio;
329
330         /* Ensure that the reset is ordered w.r.t. previous loads and stores */
331         mb();
332         /* Trigger reset */
333         reset_reg = mic_mmio_read(mw, rgcr);
334         reset_reg |= 0x1;
335         mic_mmio_write(mw, reset_reg, rgcr);
336         /*
337          * It seems we really want to delay at least 1 second
338          * after touching reset to prevent a lot of problems.
339          */
340         msleep(1000);
341 }
342
343 /**
344  * mic_x100_load_command_line - Load command line to MIC.
345  * @mdev: pointer to mic_device instance
346  * @fw: the firmware image
347  *
348  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
349  */
350 static int
351 mic_x100_load_command_line(struct mic_device *mdev, const struct firmware *fw)
352 {
353         u32 len = 0;
354         u32 boot_mem;
355         char *buf;
356         void __iomem *cmd_line_va = mdev->aper.va + mdev->bootaddr + fw->size;
357 #define CMDLINE_SIZE 2048
358
359         boot_mem = mdev->aper.len >> 20;
360         buf = kzalloc(CMDLINE_SIZE, GFP_KERNEL);
361         if (!buf)
362                 return -ENOMEM;
363
364         len += scnprintf(buf, CMDLINE_SIZE - len,
365                 " mem=%dM", boot_mem);
366         if (mdev->cosm_dev->cmdline)
367                 scnprintf(buf + len, CMDLINE_SIZE - len, " %s",
368                          mdev->cosm_dev->cmdline);
369         memcpy_toio(cmd_line_va, buf, strlen(buf) + 1);
370         kfree(buf);
371         return 0;
372 }
373
374 /**
375  * mic_x100_load_ramdisk - Load ramdisk to MIC.
376  * @mdev: pointer to mic_device instance
377  *
378  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
379  */
380 static int
381 mic_x100_load_ramdisk(struct mic_device *mdev)
382 {
383         const struct firmware *fw;
384         int rc;
385         struct boot_params __iomem *bp = mdev->aper.va + mdev->bootaddr;
386
387         rc = request_firmware(&fw, mdev->cosm_dev->ramdisk, &mdev->pdev->dev);
388         if (rc < 0) {
389                 dev_err(&mdev->pdev->dev,
390                         "ramdisk request_firmware failed: %d %s\n",
391                         rc, mdev->cosm_dev->ramdisk);
392                 goto error;
393         }
394         /*
395          * Typically the bootaddr for card OS is 64M
396          * so copy over the ramdisk @ 128M.
397          */
398         memcpy_toio(mdev->aper.va + (mdev->bootaddr << 1), fw->data, fw->size);
399         iowrite32(mdev->bootaddr << 1, &bp->hdr.ramdisk_image);
400         iowrite32(fw->size, &bp->hdr.ramdisk_size);
401         release_firmware(fw);
402 error:
403         return rc;
404 }
405
406 /**
407  * mic_x100_get_boot_addr - Get MIC boot address.
408  * @mdev: pointer to mic_device instance
409  *
410  * This function is called during firmware load to determine
411  * the address at which the OS should be downloaded in card
412  * memory i.e. GDDR.
413  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
414  */
415 static int
416 mic_x100_get_boot_addr(struct mic_device *mdev)
417 {
418         u32 scratch2, boot_addr;
419         int rc = 0;
420
421         scratch2 = mdev->ops->read_spad(mdev, MIC_X100_DOWNLOAD_INFO);
422         boot_addr = MIC_X100_SPAD2_DOWNLOAD_ADDR(scratch2);
423         dev_dbg(&mdev->pdev->dev, "%s %d boot_addr 0x%x\n",
424                 __func__, __LINE__, boot_addr);
425         if (boot_addr > (1 << 31)) {
426                 dev_err(&mdev->pdev->dev,
427                         "incorrect bootaddr 0x%x\n",
428                         boot_addr);
429                 rc = -EINVAL;
430                 goto error;
431         }
432         mdev->bootaddr = boot_addr;
433 error:
434         return rc;
435 }
436
437 /**
438  * mic_x100_load_firmware - Load firmware to MIC.
439  * @mdev: pointer to mic_device instance
440  * @buf: buffer containing boot string including firmware/ramdisk path.
441  *
442  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
443  */
444 static int
445 mic_x100_load_firmware(struct mic_device *mdev, const char *buf)
446 {
447         int rc;
448         const struct firmware *fw;
449
450         rc = mic_x100_get_boot_addr(mdev);
451         if (rc)
452                 return rc;
453         /* load OS */
454         rc = request_firmware(&fw, mdev->cosm_dev->firmware, &mdev->pdev->dev);
455         if (rc < 0) {
456                 dev_err(&mdev->pdev->dev,
457                         "ramdisk request_firmware failed: %d %s\n",
458                         rc, mdev->cosm_dev->firmware);
459                 return rc;
460         }
461         if (mdev->bootaddr > mdev->aper.len - fw->size) {
462                 rc = -EINVAL;
463                 dev_err(&mdev->pdev->dev, "%s %d rc %d bootaddr 0x%x\n",
464                         __func__, __LINE__, rc, mdev->bootaddr);
465                 goto error;
466         }
467         memcpy_toio(mdev->aper.va + mdev->bootaddr, fw->data, fw->size);
468         mdev->ops->write_spad(mdev, MIC_X100_FW_SIZE, fw->size);
469         if (!strcmp(mdev->cosm_dev->bootmode, "flash")) {
470                 rc = -EINVAL;
471                 dev_err(&mdev->pdev->dev, "%s %d rc %d\n",
472                         __func__, __LINE__, rc);
473                 goto error;
474         }
475         /* load command line */
476         rc = mic_x100_load_command_line(mdev, fw);
477         if (rc) {
478                 dev_err(&mdev->pdev->dev, "%s %d rc %d\n",
479                         __func__, __LINE__, rc);
480                 goto error;
481         }
482         release_firmware(fw);
483         /* load ramdisk */
484         if (mdev->cosm_dev->ramdisk)
485                 rc = mic_x100_load_ramdisk(mdev);
486
487         return rc;
488
489 error:
490         release_firmware(fw);
491         return rc;
492 }
493
494 /**
495  * mic_x100_get_postcode - Get postcode status from firmware.
496  * @mdev: pointer to mic_device instance
497  *
498  * RETURNS: postcode.
499  */
500 static u32 mic_x100_get_postcode(struct mic_device *mdev)
501 {
502         return mic_mmio_read(&mdev->mmio, MIC_X100_POSTCODE);
503 }
504
505 /**
506  * mic_x100_smpt_set - Update an SMPT entry with a DMA address.
507  * @mdev: pointer to mic_device instance
508  * @dma_addr: DMA address to use
509  * @index: entry to write to
510  *
511  * RETURNS: none.
512  */
513 static void
514 mic_x100_smpt_set(struct mic_device *mdev, dma_addr_t dma_addr, u8 index)
515 {
516 #define SNOOP_ON        (0 << 0)
517 #define SNOOP_OFF       (1 << 0)
518 /*
519  * Sbox Smpt Reg Bits:
520  * Bits 31:2    Host address
521  * Bits 1       RSVD
522  * Bits 0       No snoop
523  */
524 #define BUILD_SMPT(NO_SNOOP, HOST_ADDR)  \
525         (u32)(((HOST_ADDR) << 2) | ((NO_SNOOP) & 0x01))
526
527         uint32_t smpt_reg_val = BUILD_SMPT(SNOOP_ON,
528                         dma_addr >> mdev->smpt->info.page_shift);
529         mic_mmio_write(&mdev->mmio, smpt_reg_val,
530                        MIC_X100_SBOX_BASE_ADDRESS +
531                        MIC_X100_SBOX_SMPT00 + (4 * index));
532 }
533
534 /**
535  * mic_x100_smpt_hw_init - Initialize SMPT X100 specific fields.
536  * @mdev: pointer to mic_device instance
537  *
538  * RETURNS: none.
539  */
540 static void mic_x100_smpt_hw_init(struct mic_device *mdev)
541 {
542         struct mic_smpt_hw_info *info = &mdev->smpt->info;
543
544         info->num_reg = 32;
545         info->page_shift = 34;
546         info->page_size = (1ULL << info->page_shift);
547         info->base = 0x8000000000ULL;
548 }
549
550 struct mic_smpt_ops mic_x100_smpt_ops = {
551         .init = mic_x100_smpt_hw_init,
552         .set = mic_x100_smpt_set,
553 };
554
555 static bool mic_x100_dma_filter(struct dma_chan *chan, void *param)
556 {
557         if (chan->device->dev->parent == (struct device *)param)
558                 return true;
559         return false;
560 }
561
562 struct mic_hw_ops mic_x100_ops = {
563         .aper_bar = MIC_X100_APER_BAR,
564         .mmio_bar = MIC_X100_MMIO_BAR,
565         .read_spad = mic_x100_read_spad,
566         .write_spad = mic_x100_write_spad,
567         .send_intr = mic_x100_send_intr,
568         .ack_interrupt = mic_x100_ack_interrupt,
569         .intr_workarounds = mic_x100_intr_workarounds,
570         .reset = mic_x100_hw_reset,
571         .reset_fw_ready = mic_x100_reset_fw_ready,
572         .is_fw_ready = mic_x100_is_fw_ready,
573         .send_firmware_intr = mic_x100_send_firmware_intr,
574         .load_mic_fw = mic_x100_load_firmware,
575         .get_postcode = mic_x100_get_postcode,
576         .dma_filter = mic_x100_dma_filter,
577 };
578
579 struct mic_hw_intr_ops mic_x100_intr_ops = {
580         .intr_init = mic_x100_hw_intr_init,
581         .enable_interrupts = mic_x100_enable_interrupts,
582         .disable_interrupts = mic_x100_disable_interrupts,
583         .program_msi_to_src_map = mic_x100_program_msi_to_src_map,
584         .read_msi_to_src_map = mic_x100_read_msi_to_src_map,
585 };