GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / powerpc / platforms / powernv / npu-dma.c
1 /*
2  * This file implements the DMA operations for NVLink devices. The NPU
3  * devices all point to the same iommu table as the parent PCI device.
4  *
5  * Copyright Alistair Popple, IBM Corporation 2015.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU General Public
9  * License as published by the Free Software Foundation.
10  */
11
12 #include <linux/slab.h>
13 #include <linux/mmu_notifier.h>
14 #include <linux/mmu_context.h>
15 #include <linux/of.h>
16 #include <linux/export.h>
17 #include <linux/pci.h>
18 #include <linux/memblock.h>
19 #include <linux/iommu.h>
20
21 #include <asm/tlb.h>
22 #include <asm/powernv.h>
23 #include <asm/reg.h>
24 #include <asm/opal.h>
25 #include <asm/io.h>
26 #include <asm/iommu.h>
27 #include <asm/pnv-pci.h>
28 #include <asm/msi_bitmap.h>
29 #include <asm/opal.h>
30
31 #include "powernv.h"
32 #include "pci.h"
33
34 #define npu_to_phb(x) container_of(x, struct pnv_phb, npu)
35
36 /*
37  * When an address shootdown range exceeds this threshold we invalidate the
38  * entire TLB on the GPU for the given PID rather than each specific address in
39  * the range.
40  */
41 #define ATSD_THRESHOLD (2*1024*1024)
42
43 /*
44  * Other types of TCE cache invalidation are not functional in the
45  * hardware.
46  */
47 static struct pci_dev *get_pci_dev(struct device_node *dn)
48 {
49         return PCI_DN(dn)->pcidev;
50 }
51
52 /* Given a NPU device get the associated PCI device. */
53 struct pci_dev *pnv_pci_get_gpu_dev(struct pci_dev *npdev)
54 {
55         struct device_node *dn;
56         struct pci_dev *gpdev;
57
58         if (WARN_ON(!npdev))
59                 return NULL;
60
61         if (WARN_ON(!npdev->dev.of_node))
62                 return NULL;
63
64         /* Get assoicated PCI device */
65         dn = of_parse_phandle(npdev->dev.of_node, "ibm,gpu", 0);
66         if (!dn)
67                 return NULL;
68
69         gpdev = get_pci_dev(dn);
70         of_node_put(dn);
71
72         return gpdev;
73 }
74 EXPORT_SYMBOL(pnv_pci_get_gpu_dev);
75
76 /* Given the real PCI device get a linked NPU device. */
77 struct pci_dev *pnv_pci_get_npu_dev(struct pci_dev *gpdev, int index)
78 {
79         struct device_node *dn;
80         struct pci_dev *npdev;
81
82         if (WARN_ON(!gpdev))
83                 return NULL;
84
85         /* Not all PCI devices have device-tree nodes */
86         if (!gpdev->dev.of_node)
87                 return NULL;
88
89         /* Get assoicated PCI device */
90         dn = of_parse_phandle(gpdev->dev.of_node, "ibm,npu", index);
91         if (!dn)
92                 return NULL;
93
94         npdev = get_pci_dev(dn);
95         of_node_put(dn);
96
97         return npdev;
98 }
99 EXPORT_SYMBOL(pnv_pci_get_npu_dev);
100
101 #define NPU_DMA_OP_UNSUPPORTED()                                        \
102         dev_err_once(dev, "%s operation unsupported for NVLink devices\n", \
103                 __func__)
104
105 static void *dma_npu_alloc(struct device *dev, size_t size,
106                            dma_addr_t *dma_handle, gfp_t flag,
107                            unsigned long attrs)
108 {
109         NPU_DMA_OP_UNSUPPORTED();
110         return NULL;
111 }
112
113 static void dma_npu_free(struct device *dev, size_t size,
114                          void *vaddr, dma_addr_t dma_handle,
115                          unsigned long attrs)
116 {
117         NPU_DMA_OP_UNSUPPORTED();
118 }
119
120 static dma_addr_t dma_npu_map_page(struct device *dev, struct page *page,
121                                    unsigned long offset, size_t size,
122                                    enum dma_data_direction direction,
123                                    unsigned long attrs)
124 {
125         NPU_DMA_OP_UNSUPPORTED();
126         return 0;
127 }
128
129 static int dma_npu_map_sg(struct device *dev, struct scatterlist *sglist,
130                           int nelems, enum dma_data_direction direction,
131                           unsigned long attrs)
132 {
133         NPU_DMA_OP_UNSUPPORTED();
134         return 0;
135 }
136
137 static int dma_npu_dma_supported(struct device *dev, u64 mask)
138 {
139         NPU_DMA_OP_UNSUPPORTED();
140         return 0;
141 }
142
143 static u64 dma_npu_get_required_mask(struct device *dev)
144 {
145         NPU_DMA_OP_UNSUPPORTED();
146         return 0;
147 }
148
149 static const struct dma_map_ops dma_npu_ops = {
150         .map_page               = dma_npu_map_page,
151         .map_sg                 = dma_npu_map_sg,
152         .alloc                  = dma_npu_alloc,
153         .free                   = dma_npu_free,
154         .dma_supported          = dma_npu_dma_supported,
155         .get_required_mask      = dma_npu_get_required_mask,
156 };
157
158 /*
159  * Returns the PE assoicated with the PCI device of the given
160  * NPU. Returns the linked pci device if pci_dev != NULL.
161  */
162 static struct pnv_ioda_pe *get_gpu_pci_dev_and_pe(struct pnv_ioda_pe *npe,
163                                                   struct pci_dev **gpdev)
164 {
165         struct pnv_phb *phb;
166         struct pci_controller *hose;
167         struct pci_dev *pdev;
168         struct pnv_ioda_pe *pe;
169         struct pci_dn *pdn;
170
171         pdev = pnv_pci_get_gpu_dev(npe->pdev);
172         if (!pdev)
173                 return NULL;
174
175         pdn = pci_get_pdn(pdev);
176         if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
177                 return NULL;
178
179         hose = pci_bus_to_host(pdev->bus);
180         phb = hose->private_data;
181         pe = &phb->ioda.pe_array[pdn->pe_number];
182
183         if (gpdev)
184                 *gpdev = pdev;
185
186         return pe;
187 }
188
189 long pnv_npu_set_window(struct pnv_ioda_pe *npe, int num,
190                 struct iommu_table *tbl)
191 {
192         struct pnv_phb *phb = npe->phb;
193         int64_t rc;
194         const unsigned long size = tbl->it_indirect_levels ?
195                 tbl->it_level_size : tbl->it_size;
196         const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
197         const __u64 win_size = tbl->it_size << tbl->it_page_shift;
198
199         pe_info(npe, "Setting up window %llx..%llx pg=%lx\n",
200                         start_addr, start_addr + win_size - 1,
201                         IOMMU_PAGE_SIZE(tbl));
202
203         rc = opal_pci_map_pe_dma_window(phb->opal_id,
204                         npe->pe_number,
205                         npe->pe_number,
206                         tbl->it_indirect_levels + 1,
207                         __pa(tbl->it_base),
208                         size << 3,
209                         IOMMU_PAGE_SIZE(tbl));
210         if (rc) {
211                 pe_err(npe, "Failed to configure TCE table, err %lld\n", rc);
212                 return rc;
213         }
214         pnv_pci_ioda2_tce_invalidate_entire(phb, false);
215
216         /* Add the table to the list so its TCE cache will get invalidated */
217         pnv_pci_link_table_and_group(phb->hose->node, num,
218                         tbl, &npe->table_group);
219
220         return 0;
221 }
222
223 long pnv_npu_unset_window(struct pnv_ioda_pe *npe, int num)
224 {
225         struct pnv_phb *phb = npe->phb;
226         int64_t rc;
227
228         pe_info(npe, "Removing DMA window\n");
229
230         rc = opal_pci_map_pe_dma_window(phb->opal_id, npe->pe_number,
231                         npe->pe_number,
232                         0/* levels */, 0/* table address */,
233                         0/* table size */, 0/* page size */);
234         if (rc) {
235                 pe_err(npe, "Unmapping failed, ret = %lld\n", rc);
236                 return rc;
237         }
238         pnv_pci_ioda2_tce_invalidate_entire(phb, false);
239
240         pnv_pci_unlink_table_and_group(npe->table_group.tables[num],
241                         &npe->table_group);
242
243         return 0;
244 }
245
246 /*
247  * Enables 32 bit DMA on NPU.
248  */
249 static void pnv_npu_dma_set_32(struct pnv_ioda_pe *npe)
250 {
251         struct pci_dev *gpdev;
252         struct pnv_ioda_pe *gpe;
253         int64_t rc;
254
255         /*
256          * Find the assoicated PCI devices and get the dma window
257          * information from there.
258          */
259         if (!npe->pdev || !(npe->flags & PNV_IODA_PE_DEV))
260                 return;
261
262         gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
263         if (!gpe)
264                 return;
265
266         rc = pnv_npu_set_window(npe, 0, gpe->table_group.tables[0]);
267
268         /*
269          * We don't initialise npu_pe->tce32_table as we always use
270          * dma_npu_ops which are nops.
271          */
272         set_dma_ops(&npe->pdev->dev, &dma_npu_ops);
273 }
274
275 /*
276  * Enables bypass mode on the NPU. The NPU only supports one
277  * window per link, so bypass needs to be explicitly enabled or
278  * disabled. Unlike for a PHB3 bypass and non-bypass modes can't be
279  * active at the same time.
280  */
281 static int pnv_npu_dma_set_bypass(struct pnv_ioda_pe *npe)
282 {
283         struct pnv_phb *phb = npe->phb;
284         int64_t rc = 0;
285         phys_addr_t top = memblock_end_of_DRAM();
286
287         if (phb->type != PNV_PHB_NPU || !npe->pdev)
288                 return -EINVAL;
289
290         rc = pnv_npu_unset_window(npe, 0);
291         if (rc != OPAL_SUCCESS)
292                 return rc;
293
294         /* Enable the bypass window */
295
296         top = roundup_pow_of_two(top);
297         dev_info(&npe->pdev->dev, "Enabling bypass for PE %x\n",
298                         npe->pe_number);
299         rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
300                         npe->pe_number, npe->pe_number,
301                         0 /* bypass base */, top);
302
303         if (rc == OPAL_SUCCESS)
304                 pnv_pci_ioda2_tce_invalidate_entire(phb, false);
305
306         return rc;
307 }
308
309 void pnv_npu_try_dma_set_bypass(struct pci_dev *gpdev, bool bypass)
310 {
311         int i;
312         struct pnv_phb *phb;
313         struct pci_dn *pdn;
314         struct pnv_ioda_pe *npe;
315         struct pci_dev *npdev;
316
317         for (i = 0; ; ++i) {
318                 npdev = pnv_pci_get_npu_dev(gpdev, i);
319
320                 if (!npdev)
321                         break;
322
323                 pdn = pci_get_pdn(npdev);
324                 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
325                         return;
326
327                 phb = pci_bus_to_host(npdev->bus)->private_data;
328
329                 /* We only do bypass if it's enabled on the linked device */
330                 npe = &phb->ioda.pe_array[pdn->pe_number];
331
332                 if (bypass) {
333                         dev_info(&npdev->dev,
334                                         "Using 64-bit DMA iommu bypass\n");
335                         pnv_npu_dma_set_bypass(npe);
336                 } else {
337                         dev_info(&npdev->dev, "Using 32-bit DMA via iommu\n");
338                         pnv_npu_dma_set_32(npe);
339                 }
340         }
341 }
342
343 /* Switch ownership from platform code to external user (e.g. VFIO) */
344 void pnv_npu_take_ownership(struct pnv_ioda_pe *npe)
345 {
346         struct pnv_phb *phb = npe->phb;
347         int64_t rc;
348
349         /*
350          * Note: NPU has just a single TVE in the hardware which means that
351          * while used by the kernel, it can have either 32bit window or
352          * DMA bypass but never both. So we deconfigure 32bit window only
353          * if it was enabled at the moment of ownership change.
354          */
355         if (npe->table_group.tables[0]) {
356                 pnv_npu_unset_window(npe, 0);
357                 return;
358         }
359
360         /* Disable bypass */
361         rc = opal_pci_map_pe_dma_window_real(phb->opal_id,
362                         npe->pe_number, npe->pe_number,
363                         0 /* bypass base */, 0);
364         if (rc) {
365                 pe_err(npe, "Failed to disable bypass, err %lld\n", rc);
366                 return;
367         }
368         pnv_pci_ioda2_tce_invalidate_entire(npe->phb, false);
369 }
370
371 struct pnv_ioda_pe *pnv_pci_npu_setup_iommu(struct pnv_ioda_pe *npe)
372 {
373         struct pnv_phb *phb = npe->phb;
374         struct pci_bus *pbus = phb->hose->bus;
375         struct pci_dev *npdev, *gpdev = NULL, *gptmp;
376         struct pnv_ioda_pe *gpe = get_gpu_pci_dev_and_pe(npe, &gpdev);
377
378         if (!gpe || !gpdev)
379                 return NULL;
380
381         list_for_each_entry(npdev, &pbus->devices, bus_list) {
382                 gptmp = pnv_pci_get_gpu_dev(npdev);
383
384                 if (gptmp != gpdev)
385                         continue;
386
387                 pe_info(gpe, "Attached NPU %s\n", dev_name(&npdev->dev));
388                 iommu_group_add_device(gpe->table_group.group, &npdev->dev);
389         }
390
391         return gpe;
392 }
393
394 /* Maximum number of nvlinks per npu */
395 #define NV_MAX_LINKS 6
396
397 /* Maximum index of npu2 hosts in the system. Always < NV_MAX_NPUS */
398 static int max_npu2_index;
399
400 struct npu_context {
401         struct mm_struct *mm;
402         struct pci_dev *npdev[NV_MAX_NPUS][NV_MAX_LINKS];
403         struct mmu_notifier mn;
404         struct kref kref;
405
406         /* Callback to stop translation requests on a given GPU */
407         struct npu_context *(*release_cb)(struct npu_context *, void *);
408
409         /*
410          * Private pointer passed to the above callback for usage by
411          * device drivers.
412          */
413         void *priv;
414 };
415
416 struct mmio_atsd_reg {
417         struct npu *npu;
418         int reg;
419 };
420
421 /*
422  * Find a free MMIO ATSD register and mark it in use. Return -ENOSPC
423  * if none are available.
424  */
425 static int get_mmio_atsd_reg(struct npu *npu)
426 {
427         int i;
428
429         for (i = 0; i < npu->mmio_atsd_count; i++) {
430                 if (!test_bit(i, &npu->mmio_atsd_usage))
431                         if (!test_and_set_bit_lock(i, &npu->mmio_atsd_usage))
432                                 return i;
433         }
434
435         return -ENOSPC;
436 }
437
438 static void put_mmio_atsd_reg(struct npu *npu, int reg)
439 {
440         clear_bit_unlock(reg, &npu->mmio_atsd_usage);
441 }
442
443 /* MMIO ATSD register offsets */
444 #define XTS_ATSD_AVA  1
445 #define XTS_ATSD_STAT 2
446
447 static void mmio_launch_invalidate(struct mmio_atsd_reg *mmio_atsd_reg,
448                                 unsigned long launch, unsigned long va)
449 {
450         struct npu *npu = mmio_atsd_reg->npu;
451         int reg = mmio_atsd_reg->reg;
452
453         __raw_writeq(cpu_to_be64(va),
454                 npu->mmio_atsd_regs[reg] + XTS_ATSD_AVA);
455         eieio();
456         __raw_writeq(cpu_to_be64(launch), npu->mmio_atsd_regs[reg]);
457 }
458
459 static void mmio_invalidate_pid(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
460                                 unsigned long pid, bool flush)
461 {
462         int i;
463         unsigned long launch;
464
465         for (i = 0; i <= max_npu2_index; i++) {
466                 if (mmio_atsd_reg[i].reg < 0)
467                         continue;
468
469                 /* IS set to invalidate matching PID */
470                 launch = PPC_BIT(12);
471
472                 /* PRS set to process-scoped */
473                 launch |= PPC_BIT(13);
474
475                 /* AP */
476                 launch |= (u64)
477                         mmu_get_ap(mmu_virtual_psize) << PPC_BITLSHIFT(17);
478
479                 /* PID */
480                 launch |= pid << PPC_BITLSHIFT(38);
481
482                 /* No flush */
483                 launch |= !flush << PPC_BITLSHIFT(39);
484
485                 /* Invalidating the entire process doesn't use a va */
486                 mmio_launch_invalidate(&mmio_atsd_reg[i], launch, 0);
487         }
488 }
489
490 static void mmio_invalidate_va(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS],
491                         unsigned long va, unsigned long pid, bool flush)
492 {
493         int i;
494         unsigned long launch;
495
496         for (i = 0; i <= max_npu2_index; i++) {
497                 if (mmio_atsd_reg[i].reg < 0)
498                         continue;
499
500                 /* IS set to invalidate target VA */
501                 launch = 0;
502
503                 /* PRS set to process scoped */
504                 launch |= PPC_BIT(13);
505
506                 /* AP */
507                 launch |= (u64)
508                         mmu_get_ap(mmu_virtual_psize) << PPC_BITLSHIFT(17);
509
510                 /* PID */
511                 launch |= pid << PPC_BITLSHIFT(38);
512
513                 /* No flush */
514                 launch |= !flush << PPC_BITLSHIFT(39);
515
516                 mmio_launch_invalidate(&mmio_atsd_reg[i], launch, va);
517         }
518 }
519
520 #define mn_to_npu_context(x) container_of(x, struct npu_context, mn)
521
522 static void mmio_invalidate_wait(
523         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
524 {
525         struct npu *npu;
526         int i, reg;
527
528         /* Wait for all invalidations to complete */
529         for (i = 0; i <= max_npu2_index; i++) {
530                 if (mmio_atsd_reg[i].reg < 0)
531                         continue;
532
533                 /* Wait for completion */
534                 npu = mmio_atsd_reg[i].npu;
535                 reg = mmio_atsd_reg[i].reg;
536                 while (__raw_readq(npu->mmio_atsd_regs[reg] + XTS_ATSD_STAT))
537                         cpu_relax();
538         }
539 }
540
541 /*
542  * Acquires all the address translation shootdown (ATSD) registers required to
543  * launch an ATSD on all links this npu_context is active on.
544  */
545 static void acquire_atsd_reg(struct npu_context *npu_context,
546                         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
547 {
548         int i, j;
549         struct npu *npu;
550         struct pci_dev *npdev;
551         struct pnv_phb *nphb;
552
553         for (i = 0; i <= max_npu2_index; i++) {
554                 mmio_atsd_reg[i].reg = -1;
555                 for (j = 0; j < NV_MAX_LINKS; j++) {
556                         /*
557                          * There are no ordering requirements with respect to
558                          * the setup of struct npu_context, but to ensure
559                          * consistent behaviour we need to ensure npdev[][] is
560                          * only read once.
561                          */
562                         npdev = READ_ONCE(npu_context->npdev[i][j]);
563                         if (!npdev)
564                                 continue;
565
566                         nphb = pci_bus_to_host(npdev->bus)->private_data;
567                         npu = &nphb->npu;
568                         mmio_atsd_reg[i].npu = npu;
569                         mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
570                         while (mmio_atsd_reg[i].reg < 0) {
571                                 mmio_atsd_reg[i].reg = get_mmio_atsd_reg(npu);
572                                 cpu_relax();
573                         }
574                         break;
575                 }
576         }
577 }
578
579 /*
580  * Release previously acquired ATSD registers. To avoid deadlocks the registers
581  * must be released in the same order they were acquired above in
582  * acquire_atsd_reg.
583  */
584 static void release_atsd_reg(struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS])
585 {
586         int i;
587
588         for (i = 0; i <= max_npu2_index; i++) {
589                 /*
590                  * We can't rely on npu_context->npdev[][] being the same here
591                  * as when acquire_atsd_reg() was called, hence we use the
592                  * values stored in mmio_atsd_reg during the acquire phase
593                  * rather than re-reading npdev[][].
594                  */
595                 if (mmio_atsd_reg[i].reg < 0)
596                         continue;
597
598                 put_mmio_atsd_reg(mmio_atsd_reg[i].npu, mmio_atsd_reg[i].reg);
599         }
600 }
601
602 /*
603  * Invalidate either a single address or an entire PID depending on
604  * the value of va.
605  */
606 static void mmio_invalidate(struct npu_context *npu_context, int va,
607                         unsigned long address, bool flush)
608 {
609         struct mmio_atsd_reg mmio_atsd_reg[NV_MAX_NPUS];
610         unsigned long pid = npu_context->mm->context.id;
611
612         /*
613          * Unfortunately the nest mmu does not support flushing specific
614          * addresses so we have to flush the whole mm.
615          */
616         flush_tlb_mm(npu_context->mm);
617
618         /*
619          * Loop over all the NPUs this process is active on and launch
620          * an invalidate.
621          */
622         acquire_atsd_reg(npu_context, mmio_atsd_reg);
623         if (va)
624                 mmio_invalidate_va(mmio_atsd_reg, address, pid, flush);
625         else
626                 mmio_invalidate_pid(mmio_atsd_reg, pid, flush);
627
628         mmio_invalidate_wait(mmio_atsd_reg);
629         if (flush) {
630                 /*
631                  * The GPU requires two flush ATSDs to ensure all entries have
632                  * been flushed. We use PID 0 as it will never be used for a
633                  * process on the GPU.
634                  */
635                 mmio_invalidate_pid(mmio_atsd_reg, 0, true);
636                 mmio_invalidate_wait(mmio_atsd_reg);
637                 mmio_invalidate_pid(mmio_atsd_reg, 0, true);
638                 mmio_invalidate_wait(mmio_atsd_reg);
639         }
640         release_atsd_reg(mmio_atsd_reg);
641 }
642
643 static void pnv_npu2_mn_release(struct mmu_notifier *mn,
644                                 struct mm_struct *mm)
645 {
646         struct npu_context *npu_context = mn_to_npu_context(mn);
647
648         /* Call into device driver to stop requests to the NMMU */
649         if (npu_context->release_cb)
650                 npu_context->release_cb(npu_context, npu_context->priv);
651
652         /*
653          * There should be no more translation requests for this PID, but we
654          * need to ensure any entries for it are removed from the TLB.
655          */
656         mmio_invalidate(npu_context, 0, 0, true);
657 }
658
659 static void pnv_npu2_mn_change_pte(struct mmu_notifier *mn,
660                                 struct mm_struct *mm,
661                                 unsigned long address,
662                                 pte_t pte)
663 {
664         struct npu_context *npu_context = mn_to_npu_context(mn);
665
666         mmio_invalidate(npu_context, 1, address, true);
667 }
668
669 static void pnv_npu2_mn_invalidate_range(struct mmu_notifier *mn,
670                                         struct mm_struct *mm,
671                                         unsigned long start, unsigned long end)
672 {
673         struct npu_context *npu_context = mn_to_npu_context(mn);
674         unsigned long address;
675
676         if (end - start > ATSD_THRESHOLD) {
677                 /*
678                  * Just invalidate the entire PID if the address range is too
679                  * large.
680                  */
681                 mmio_invalidate(npu_context, 0, 0, true);
682         } else {
683                 for (address = start; address < end; address += PAGE_SIZE)
684                         mmio_invalidate(npu_context, 1, address, false);
685
686                 /* Do the flush only on the final addess == end */
687                 mmio_invalidate(npu_context, 1, address, true);
688         }
689 }
690
691 static const struct mmu_notifier_ops nv_nmmu_notifier_ops = {
692         .release = pnv_npu2_mn_release,
693         .change_pte = pnv_npu2_mn_change_pte,
694         .invalidate_range = pnv_npu2_mn_invalidate_range,
695 };
696
697 /*
698  * Call into OPAL to setup the nmmu context for the current task in
699  * the NPU. This must be called to setup the context tables before the
700  * GPU issues ATRs. pdev should be a pointed to PCIe GPU device.
701  *
702  * A release callback should be registered to allow a device driver to
703  * be notified that it should not launch any new translation requests
704  * as the final TLB invalidate is about to occur.
705  *
706  * Returns an error if there no contexts are currently available or a
707  * npu_context which should be passed to pnv_npu2_handle_fault().
708  *
709  * mmap_sem must be held in write mode.
710  */
711 struct npu_context *pnv_npu2_init_context(struct pci_dev *gpdev,
712                         unsigned long flags,
713                         struct npu_context *(*cb)(struct npu_context *, void *),
714                         void *priv)
715 {
716         int rc;
717         u32 nvlink_index;
718         struct device_node *nvlink_dn;
719         struct mm_struct *mm = current->mm;
720         struct pnv_phb *nphb;
721         struct npu *npu;
722         struct npu_context *npu_context;
723
724         /*
725          * At present we don't support GPUs connected to multiple NPUs and I'm
726          * not sure the hardware does either.
727          */
728         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
729
730         if (!firmware_has_feature(FW_FEATURE_OPAL))
731                 return ERR_PTR(-ENODEV);
732
733         if (!npdev)
734                 /* No nvlink associated with this GPU device */
735                 return ERR_PTR(-ENODEV);
736
737         if (!mm || mm->context.id == 0) {
738                 /*
739                  * Kernel thread contexts are not supported and context id 0 is
740                  * reserved on the GPU.
741                  */
742                 return ERR_PTR(-EINVAL);
743         }
744
745         nphb = pci_bus_to_host(npdev->bus)->private_data;
746         npu = &nphb->npu;
747
748         /*
749          * Setup the NPU context table for a particular GPU. These need to be
750          * per-GPU as we need the tables to filter ATSDs when there are no
751          * active contexts on a particular GPU.
752          */
753         rc = opal_npu_init_context(nphb->opal_id, mm->context.id, flags,
754                                 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
755         if (rc < 0)
756                 return ERR_PTR(-ENOSPC);
757
758         /*
759          * We store the npu pci device so we can more easily get at the
760          * associated npus.
761          */
762         npu_context = mm->context.npu_context;
763         if (!npu_context) {
764                 npu_context = kzalloc(sizeof(struct npu_context), GFP_KERNEL);
765                 if (!npu_context)
766                         return ERR_PTR(-ENOMEM);
767
768                 mm->context.npu_context = npu_context;
769                 npu_context->mm = mm;
770                 npu_context->mn.ops = &nv_nmmu_notifier_ops;
771                 __mmu_notifier_register(&npu_context->mn, mm);
772                 kref_init(&npu_context->kref);
773         } else {
774                 kref_get(&npu_context->kref);
775         }
776
777         npu_context->release_cb = cb;
778         npu_context->priv = priv;
779         nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
780         if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
781                                                         &nvlink_index)))
782                 return ERR_PTR(-ENODEV);
783
784         /*
785          * npdev is a pci_dev pointer setup by the PCI code. We assign it to
786          * npdev[][] to indicate to the mmu notifiers that an invalidation
787          * should also be sent over this nvlink. The notifiers don't use any
788          * other fields in npu_context, so we just need to ensure that when they
789          * deference npu_context->npdev[][] it is either a valid pointer or
790          * NULL.
791          */
792         WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], npdev);
793
794         return npu_context;
795 }
796 EXPORT_SYMBOL(pnv_npu2_init_context);
797
798 static void pnv_npu2_release_context(struct kref *kref)
799 {
800         struct npu_context *npu_context =
801                 container_of(kref, struct npu_context, kref);
802
803         npu_context->mm->context.npu_context = NULL;
804         mmu_notifier_unregister(&npu_context->mn,
805                                 npu_context->mm);
806
807         kfree(npu_context);
808 }
809
810 void pnv_npu2_destroy_context(struct npu_context *npu_context,
811                         struct pci_dev *gpdev)
812 {
813         struct pnv_phb *nphb;
814         struct npu *npu;
815         struct pci_dev *npdev = pnv_pci_get_npu_dev(gpdev, 0);
816         struct device_node *nvlink_dn;
817         u32 nvlink_index;
818
819         if (WARN_ON(!npdev))
820                 return;
821
822         if (!firmware_has_feature(FW_FEATURE_OPAL))
823                 return;
824
825         nphb = pci_bus_to_host(npdev->bus)->private_data;
826         npu = &nphb->npu;
827         nvlink_dn = of_parse_phandle(npdev->dev.of_node, "ibm,nvlink", 0);
828         if (WARN_ON(of_property_read_u32(nvlink_dn, "ibm,npu-link-index",
829                                                         &nvlink_index)))
830                 return;
831         WRITE_ONCE(npu_context->npdev[npu->index][nvlink_index], NULL);
832         opal_npu_destroy_context(nphb->opal_id, npu_context->mm->context.id,
833                                 PCI_DEVID(gpdev->bus->number, gpdev->devfn));
834         kref_put(&npu_context->kref, pnv_npu2_release_context);
835 }
836 EXPORT_SYMBOL(pnv_npu2_destroy_context);
837
838 /*
839  * Assumes mmap_sem is held for the contexts associated mm.
840  */
841 int pnv_npu2_handle_fault(struct npu_context *context, uintptr_t *ea,
842                         unsigned long *flags, unsigned long *status, int count)
843 {
844         u64 rc = 0, result = 0;
845         int i, is_write;
846         struct page *page[1];
847
848         /* mmap_sem should be held so the struct_mm must be present */
849         struct mm_struct *mm = context->mm;
850
851         if (!firmware_has_feature(FW_FEATURE_OPAL))
852                 return -ENODEV;
853
854         WARN_ON(!rwsem_is_locked(&mm->mmap_sem));
855
856         for (i = 0; i < count; i++) {
857                 is_write = flags[i] & NPU2_WRITE;
858                 rc = get_user_pages_remote(NULL, mm, ea[i], 1,
859                                         is_write ? FOLL_WRITE : 0,
860                                         page, NULL, NULL);
861
862                 /*
863                  * To support virtualised environments we will have to do an
864                  * access to the page to ensure it gets faulted into the
865                  * hypervisor. For the moment virtualisation is not supported in
866                  * other areas so leave the access out.
867                  */
868                 if (rc != 1) {
869                         status[i] = rc;
870                         result = -EFAULT;
871                         continue;
872                 }
873
874                 status[i] = 0;
875                 put_page(page[0]);
876         }
877
878         return result;
879 }
880 EXPORT_SYMBOL(pnv_npu2_handle_fault);
881
882 int pnv_npu2_init(struct pnv_phb *phb)
883 {
884         unsigned int i;
885         u64 mmio_atsd;
886         struct device_node *dn;
887         struct pci_dev *gpdev;
888         static int npu_index;
889         uint64_t rc = 0;
890
891         for_each_child_of_node(phb->hose->dn, dn) {
892                 gpdev = pnv_pci_get_gpu_dev(get_pci_dev(dn));
893                 if (gpdev) {
894                         rc = opal_npu_map_lpar(phb->opal_id,
895                                 PCI_DEVID(gpdev->bus->number, gpdev->devfn),
896                                 0, 0);
897                         if (rc)
898                                 dev_err(&gpdev->dev,
899                                         "Error %lld mapping device to LPAR\n",
900                                         rc);
901                 }
902         }
903
904         for (i = 0; !of_property_read_u64_index(phb->hose->dn, "ibm,mmio-atsd",
905                                                         i, &mmio_atsd); i++)
906                 phb->npu.mmio_atsd_regs[i] = ioremap(mmio_atsd, 32);
907
908         pr_info("NPU%lld: Found %d MMIO ATSD registers", phb->opal_id, i);
909         phb->npu.mmio_atsd_count = i;
910         phb->npu.mmio_atsd_usage = 0;
911         npu_index++;
912         if (WARN_ON(npu_index >= NV_MAX_NPUS))
913                 return -ENOSPC;
914         max_npu2_index = npu_index;
915         phb->npu.index = npu_index;
916
917         return 0;
918 }