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