xref: /openbmc/linux/drivers/misc/ocxl/link.c (revision f5ad1c74)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/sched/mm.h>
4 #include <linux/mutex.h>
5 #include <linux/mm_types.h>
6 #include <linux/mmu_context.h>
7 #include <asm/copro.h>
8 #include <asm/pnv-ocxl.h>
9 #include <asm/xive.h>
10 #include <misc/ocxl.h>
11 #include "ocxl_internal.h"
12 #include "trace.h"
13 
14 
15 #define SPA_PASID_BITS		15
16 #define SPA_PASID_MAX		((1 << SPA_PASID_BITS) - 1)
17 #define SPA_PE_MASK		SPA_PASID_MAX
18 #define SPA_SPA_SIZE_LOG	22 /* Each SPA is 4 Mb */
19 
20 #define SPA_CFG_SF		(1ull << (63-0))
21 #define SPA_CFG_TA		(1ull << (63-1))
22 #define SPA_CFG_HV		(1ull << (63-3))
23 #define SPA_CFG_UV		(1ull << (63-4))
24 #define SPA_CFG_XLAT_hpt	(0ull << (63-6)) /* Hashed page table (HPT) mode */
25 #define SPA_CFG_XLAT_roh	(2ull << (63-6)) /* Radix on HPT mode */
26 #define SPA_CFG_XLAT_ror	(3ull << (63-6)) /* Radix on Radix mode */
27 #define SPA_CFG_PR		(1ull << (63-49))
28 #define SPA_CFG_TC		(1ull << (63-54))
29 #define SPA_CFG_DR		(1ull << (63-59))
30 
31 #define SPA_XSL_TF		(1ull << (63-3))  /* Translation fault */
32 #define SPA_XSL_S		(1ull << (63-38)) /* Store operation */
33 
34 #define SPA_PE_VALID		0x80000000
35 
36 
37 struct pe_data {
38 	struct mm_struct *mm;
39 	/* callback to trigger when a translation fault occurs */
40 	void (*xsl_err_cb)(void *data, u64 addr, u64 dsisr);
41 	/* opaque pointer to be passed to the above callback */
42 	void *xsl_err_data;
43 	struct rcu_head rcu;
44 };
45 
46 struct spa {
47 	struct ocxl_process_element *spa_mem;
48 	int spa_order;
49 	struct mutex spa_lock;
50 	struct radix_tree_root pe_tree; /* Maps PE handles to pe_data */
51 	char *irq_name;
52 	int virq;
53 	void __iomem *reg_dsisr;
54 	void __iomem *reg_dar;
55 	void __iomem *reg_tfc;
56 	void __iomem *reg_pe_handle;
57 	/*
58 	 * The following field are used by the memory fault
59 	 * interrupt handler. We can only have one interrupt at a
60 	 * time. The NPU won't raise another interrupt until the
61 	 * previous one has been ack'd by writing to the TFC register
62 	 */
63 	struct xsl_fault {
64 		struct work_struct fault_work;
65 		u64 pe;
66 		u64 dsisr;
67 		u64 dar;
68 		struct pe_data pe_data;
69 	} xsl_fault;
70 };
71 
72 /*
73  * A opencapi link can be used be by several PCI functions. We have
74  * one link per device slot.
75  *
76  * A linked list of opencapi links should suffice, as there's a
77  * limited number of opencapi slots on a system and lookup is only
78  * done when the device is probed
79  */
80 struct ocxl_link {
81 	struct list_head list;
82 	struct kref ref;
83 	int domain;
84 	int bus;
85 	int dev;
86 	atomic_t irq_available;
87 	struct spa *spa;
88 	void *platform_data;
89 };
90 static struct list_head links_list = LIST_HEAD_INIT(links_list);
91 static DEFINE_MUTEX(links_list_lock);
92 
93 enum xsl_response {
94 	CONTINUE,
95 	ADDRESS_ERROR,
96 	RESTART,
97 };
98 
99 
100 static void read_irq(struct spa *spa, u64 *dsisr, u64 *dar, u64 *pe)
101 {
102 	u64 reg;
103 
104 	*dsisr = in_be64(spa->reg_dsisr);
105 	*dar = in_be64(spa->reg_dar);
106 	reg = in_be64(spa->reg_pe_handle);
107 	*pe = reg & SPA_PE_MASK;
108 }
109 
110 static void ack_irq(struct spa *spa, enum xsl_response r)
111 {
112 	u64 reg = 0;
113 
114 	/* continue is not supported */
115 	if (r == RESTART)
116 		reg = PPC_BIT(31);
117 	else if (r == ADDRESS_ERROR)
118 		reg = PPC_BIT(30);
119 	else
120 		WARN(1, "Invalid irq response %d\n", r);
121 
122 	if (reg) {
123 		trace_ocxl_fault_ack(spa->spa_mem, spa->xsl_fault.pe,
124 				spa->xsl_fault.dsisr, spa->xsl_fault.dar, reg);
125 		out_be64(spa->reg_tfc, reg);
126 	}
127 }
128 
129 static void xsl_fault_handler_bh(struct work_struct *fault_work)
130 {
131 	vm_fault_t flt = 0;
132 	unsigned long access, flags, inv_flags = 0;
133 	enum xsl_response r;
134 	struct xsl_fault *fault = container_of(fault_work, struct xsl_fault,
135 					fault_work);
136 	struct spa *spa = container_of(fault, struct spa, xsl_fault);
137 
138 	int rc;
139 
140 	/*
141 	 * We must release a reference on mm_users whenever exiting this
142 	 * function (taken in the memory fault interrupt handler)
143 	 */
144 	rc = copro_handle_mm_fault(fault->pe_data.mm, fault->dar, fault->dsisr,
145 				&flt);
146 	if (rc) {
147 		pr_debug("copro_handle_mm_fault failed: %d\n", rc);
148 		if (fault->pe_data.xsl_err_cb) {
149 			fault->pe_data.xsl_err_cb(
150 				fault->pe_data.xsl_err_data,
151 				fault->dar, fault->dsisr);
152 		}
153 		r = ADDRESS_ERROR;
154 		goto ack;
155 	}
156 
157 	if (!radix_enabled()) {
158 		/*
159 		 * update_mmu_cache() will not have loaded the hash
160 		 * since current->trap is not a 0x400 or 0x300, so
161 		 * just call hash_page_mm() here.
162 		 */
163 		access = _PAGE_PRESENT | _PAGE_READ;
164 		if (fault->dsisr & SPA_XSL_S)
165 			access |= _PAGE_WRITE;
166 
167 		if (get_region_id(fault->dar) != USER_REGION_ID)
168 			access |= _PAGE_PRIVILEGED;
169 
170 		local_irq_save(flags);
171 		hash_page_mm(fault->pe_data.mm, fault->dar, access, 0x300,
172 			inv_flags);
173 		local_irq_restore(flags);
174 	}
175 	r = RESTART;
176 ack:
177 	mmput(fault->pe_data.mm);
178 	ack_irq(spa, r);
179 }
180 
181 static irqreturn_t xsl_fault_handler(int irq, void *data)
182 {
183 	struct ocxl_link *link = (struct ocxl_link *) data;
184 	struct spa *spa = link->spa;
185 	u64 dsisr, dar, pe_handle;
186 	struct pe_data *pe_data;
187 	struct ocxl_process_element *pe;
188 	int pid;
189 	bool schedule = false;
190 
191 	read_irq(spa, &dsisr, &dar, &pe_handle);
192 	trace_ocxl_fault(spa->spa_mem, pe_handle, dsisr, dar, -1);
193 
194 	WARN_ON(pe_handle > SPA_PE_MASK);
195 	pe = spa->spa_mem + pe_handle;
196 	pid = be32_to_cpu(pe->pid);
197 	/* We could be reading all null values here if the PE is being
198 	 * removed while an interrupt kicks in. It's not supposed to
199 	 * happen if the driver notified the AFU to terminate the
200 	 * PASID, and the AFU waited for pending operations before
201 	 * acknowledging. But even if it happens, we won't find a
202 	 * memory context below and fail silently, so it should be ok.
203 	 */
204 	if (!(dsisr & SPA_XSL_TF)) {
205 		WARN(1, "Invalid xsl interrupt fault register %#llx\n", dsisr);
206 		ack_irq(spa, ADDRESS_ERROR);
207 		return IRQ_HANDLED;
208 	}
209 
210 	rcu_read_lock();
211 	pe_data = radix_tree_lookup(&spa->pe_tree, pe_handle);
212 	if (!pe_data) {
213 		/*
214 		 * Could only happen if the driver didn't notify the
215 		 * AFU about PASID termination before removing the PE,
216 		 * or the AFU didn't wait for all memory access to
217 		 * have completed.
218 		 *
219 		 * Either way, we fail early, but we shouldn't log an
220 		 * error message, as it is a valid (if unexpected)
221 		 * scenario
222 		 */
223 		rcu_read_unlock();
224 		pr_debug("Unknown mm context for xsl interrupt\n");
225 		ack_irq(spa, ADDRESS_ERROR);
226 		return IRQ_HANDLED;
227 	}
228 
229 	if (!pe_data->mm) {
230 		/*
231 		 * translation fault from a kernel context - an OpenCAPI
232 		 * device tried to access a bad kernel address
233 		 */
234 		rcu_read_unlock();
235 		pr_warn("Unresolved OpenCAPI xsl fault in kernel context\n");
236 		ack_irq(spa, ADDRESS_ERROR);
237 		return IRQ_HANDLED;
238 	}
239 	WARN_ON(pe_data->mm->context.id != pid);
240 
241 	if (mmget_not_zero(pe_data->mm)) {
242 			spa->xsl_fault.pe = pe_handle;
243 			spa->xsl_fault.dar = dar;
244 			spa->xsl_fault.dsisr = dsisr;
245 			spa->xsl_fault.pe_data = *pe_data;
246 			schedule = true;
247 			/* mm_users count released by bottom half */
248 	}
249 	rcu_read_unlock();
250 	if (schedule)
251 		schedule_work(&spa->xsl_fault.fault_work);
252 	else
253 		ack_irq(spa, ADDRESS_ERROR);
254 	return IRQ_HANDLED;
255 }
256 
257 static void unmap_irq_registers(struct spa *spa)
258 {
259 	pnv_ocxl_unmap_xsl_regs(spa->reg_dsisr, spa->reg_dar, spa->reg_tfc,
260 				spa->reg_pe_handle);
261 }
262 
263 static int map_irq_registers(struct pci_dev *dev, struct spa *spa)
264 {
265 	return pnv_ocxl_map_xsl_regs(dev, &spa->reg_dsisr, &spa->reg_dar,
266 				&spa->reg_tfc, &spa->reg_pe_handle);
267 }
268 
269 static int setup_xsl_irq(struct pci_dev *dev, struct ocxl_link *link)
270 {
271 	struct spa *spa = link->spa;
272 	int rc;
273 	int hwirq;
274 
275 	rc = pnv_ocxl_get_xsl_irq(dev, &hwirq);
276 	if (rc)
277 		return rc;
278 
279 	rc = map_irq_registers(dev, spa);
280 	if (rc)
281 		return rc;
282 
283 	spa->irq_name = kasprintf(GFP_KERNEL, "ocxl-xsl-%x-%x-%x",
284 				link->domain, link->bus, link->dev);
285 	if (!spa->irq_name) {
286 		dev_err(&dev->dev, "Can't allocate name for xsl interrupt\n");
287 		rc = -ENOMEM;
288 		goto err_xsl;
289 	}
290 	/*
291 	 * At some point, we'll need to look into allowing a higher
292 	 * number of interrupts. Could we have an IRQ domain per link?
293 	 */
294 	spa->virq = irq_create_mapping(NULL, hwirq);
295 	if (!spa->virq) {
296 		dev_err(&dev->dev,
297 			"irq_create_mapping failed for translation interrupt\n");
298 		rc = -EINVAL;
299 		goto err_name;
300 	}
301 
302 	dev_dbg(&dev->dev, "hwirq %d mapped to virq %d\n", hwirq, spa->virq);
303 
304 	rc = request_irq(spa->virq, xsl_fault_handler, 0, spa->irq_name,
305 			link);
306 	if (rc) {
307 		dev_err(&dev->dev,
308 			"request_irq failed for translation interrupt: %d\n",
309 			rc);
310 		rc = -EINVAL;
311 		goto err_mapping;
312 	}
313 	return 0;
314 
315 err_mapping:
316 	irq_dispose_mapping(spa->virq);
317 err_name:
318 	kfree(spa->irq_name);
319 err_xsl:
320 	unmap_irq_registers(spa);
321 	return rc;
322 }
323 
324 static void release_xsl_irq(struct ocxl_link *link)
325 {
326 	struct spa *spa = link->spa;
327 
328 	if (spa->virq) {
329 		free_irq(spa->virq, link);
330 		irq_dispose_mapping(spa->virq);
331 	}
332 	kfree(spa->irq_name);
333 	unmap_irq_registers(spa);
334 }
335 
336 static int alloc_spa(struct pci_dev *dev, struct ocxl_link *link)
337 {
338 	struct spa *spa;
339 
340 	spa = kzalloc(sizeof(struct spa), GFP_KERNEL);
341 	if (!spa)
342 		return -ENOMEM;
343 
344 	mutex_init(&spa->spa_lock);
345 	INIT_RADIX_TREE(&spa->pe_tree, GFP_KERNEL);
346 	INIT_WORK(&spa->xsl_fault.fault_work, xsl_fault_handler_bh);
347 
348 	spa->spa_order = SPA_SPA_SIZE_LOG - PAGE_SHIFT;
349 	spa->spa_mem = (struct ocxl_process_element *)
350 		__get_free_pages(GFP_KERNEL | __GFP_ZERO, spa->spa_order);
351 	if (!spa->spa_mem) {
352 		dev_err(&dev->dev, "Can't allocate Shared Process Area\n");
353 		kfree(spa);
354 		return -ENOMEM;
355 	}
356 	pr_debug("Allocated SPA for %x:%x:%x at %p\n", link->domain, link->bus,
357 		link->dev, spa->spa_mem);
358 
359 	link->spa = spa;
360 	return 0;
361 }
362 
363 static void free_spa(struct ocxl_link *link)
364 {
365 	struct spa *spa = link->spa;
366 
367 	pr_debug("Freeing SPA for %x:%x:%x\n", link->domain, link->bus,
368 		link->dev);
369 
370 	if (spa && spa->spa_mem) {
371 		free_pages((unsigned long) spa->spa_mem, spa->spa_order);
372 		kfree(spa);
373 		link->spa = NULL;
374 	}
375 }
376 
377 static int alloc_link(struct pci_dev *dev, int PE_mask, struct ocxl_link **out_link)
378 {
379 	struct ocxl_link *link;
380 	int rc;
381 
382 	link = kzalloc(sizeof(struct ocxl_link), GFP_KERNEL);
383 	if (!link)
384 		return -ENOMEM;
385 
386 	kref_init(&link->ref);
387 	link->domain = pci_domain_nr(dev->bus);
388 	link->bus = dev->bus->number;
389 	link->dev = PCI_SLOT(dev->devfn);
390 	atomic_set(&link->irq_available, MAX_IRQ_PER_LINK);
391 
392 	rc = alloc_spa(dev, link);
393 	if (rc)
394 		goto err_free;
395 
396 	rc = setup_xsl_irq(dev, link);
397 	if (rc)
398 		goto err_spa;
399 
400 	/* platform specific hook */
401 	rc = pnv_ocxl_spa_setup(dev, link->spa->spa_mem, PE_mask,
402 				&link->platform_data);
403 	if (rc)
404 		goto err_xsl_irq;
405 
406 	*out_link = link;
407 	return 0;
408 
409 err_xsl_irq:
410 	release_xsl_irq(link);
411 err_spa:
412 	free_spa(link);
413 err_free:
414 	kfree(link);
415 	return rc;
416 }
417 
418 static void free_link(struct ocxl_link *link)
419 {
420 	release_xsl_irq(link);
421 	free_spa(link);
422 	kfree(link);
423 }
424 
425 int ocxl_link_setup(struct pci_dev *dev, int PE_mask, void **link_handle)
426 {
427 	int rc = 0;
428 	struct ocxl_link *link;
429 
430 	mutex_lock(&links_list_lock);
431 	list_for_each_entry(link, &links_list, list) {
432 		/* The functions of a device all share the same link */
433 		if (link->domain == pci_domain_nr(dev->bus) &&
434 			link->bus == dev->bus->number &&
435 			link->dev == PCI_SLOT(dev->devfn)) {
436 			kref_get(&link->ref);
437 			*link_handle = link;
438 			goto unlock;
439 		}
440 	}
441 	rc = alloc_link(dev, PE_mask, &link);
442 	if (rc)
443 		goto unlock;
444 
445 	list_add(&link->list, &links_list);
446 	*link_handle = link;
447 unlock:
448 	mutex_unlock(&links_list_lock);
449 	return rc;
450 }
451 EXPORT_SYMBOL_GPL(ocxl_link_setup);
452 
453 static void release_xsl(struct kref *ref)
454 {
455 	struct ocxl_link *link = container_of(ref, struct ocxl_link, ref);
456 
457 	list_del(&link->list);
458 	/* call platform code before releasing data */
459 	pnv_ocxl_spa_release(link->platform_data);
460 	free_link(link);
461 }
462 
463 void ocxl_link_release(struct pci_dev *dev, void *link_handle)
464 {
465 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
466 
467 	mutex_lock(&links_list_lock);
468 	kref_put(&link->ref, release_xsl);
469 	mutex_unlock(&links_list_lock);
470 }
471 EXPORT_SYMBOL_GPL(ocxl_link_release);
472 
473 static u64 calculate_cfg_state(bool kernel)
474 {
475 	u64 state;
476 
477 	state = SPA_CFG_DR;
478 	if (mfspr(SPRN_LPCR) & LPCR_TC)
479 		state |= SPA_CFG_TC;
480 	if (radix_enabled())
481 		state |= SPA_CFG_XLAT_ror;
482 	else
483 		state |= SPA_CFG_XLAT_hpt;
484 	state |= SPA_CFG_HV;
485 	if (kernel) {
486 		if (mfmsr() & MSR_SF)
487 			state |= SPA_CFG_SF;
488 	} else {
489 		state |= SPA_CFG_PR;
490 		if (!test_tsk_thread_flag(current, TIF_32BIT))
491 			state |= SPA_CFG_SF;
492 	}
493 	return state;
494 }
495 
496 int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
497 		u64 amr, struct mm_struct *mm,
498 		void (*xsl_err_cb)(void *data, u64 addr, u64 dsisr),
499 		void *xsl_err_data)
500 {
501 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
502 	struct spa *spa = link->spa;
503 	struct ocxl_process_element *pe;
504 	int pe_handle, rc = 0;
505 	struct pe_data *pe_data;
506 
507 	BUILD_BUG_ON(sizeof(struct ocxl_process_element) != 128);
508 	if (pasid > SPA_PASID_MAX)
509 		return -EINVAL;
510 
511 	mutex_lock(&spa->spa_lock);
512 	pe_handle = pasid & SPA_PE_MASK;
513 	pe = spa->spa_mem + pe_handle;
514 
515 	if (pe->software_state) {
516 		rc = -EBUSY;
517 		goto unlock;
518 	}
519 
520 	pe_data = kmalloc(sizeof(*pe_data), GFP_KERNEL);
521 	if (!pe_data) {
522 		rc = -ENOMEM;
523 		goto unlock;
524 	}
525 
526 	pe_data->mm = mm;
527 	pe_data->xsl_err_cb = xsl_err_cb;
528 	pe_data->xsl_err_data = xsl_err_data;
529 
530 	memset(pe, 0, sizeof(struct ocxl_process_element));
531 	pe->config_state = cpu_to_be64(calculate_cfg_state(pidr == 0));
532 	pe->lpid = cpu_to_be32(mfspr(SPRN_LPID));
533 	pe->pid = cpu_to_be32(pidr);
534 	pe->tid = cpu_to_be32(tidr);
535 	pe->amr = cpu_to_be64(amr);
536 	pe->software_state = cpu_to_be32(SPA_PE_VALID);
537 
538 	/*
539 	 * For user contexts, register a copro so that TLBIs are seen
540 	 * by the nest MMU. If we have a kernel context, TLBIs are
541 	 * already global.
542 	 */
543 	if (mm)
544 		mm_context_add_copro(mm);
545 	/*
546 	 * Barrier is to make sure PE is visible in the SPA before it
547 	 * is used by the device. It also helps with the global TLBI
548 	 * invalidation
549 	 */
550 	mb();
551 	radix_tree_insert(&spa->pe_tree, pe_handle, pe_data);
552 
553 	/*
554 	 * The mm must stay valid for as long as the device uses it. We
555 	 * lower the count when the context is removed from the SPA.
556 	 *
557 	 * We grab mm_count (and not mm_users), as we don't want to
558 	 * end up in a circular dependency if a process mmaps its
559 	 * mmio, therefore incrementing the file ref count when
560 	 * calling mmap(), and forgets to unmap before exiting. In
561 	 * that scenario, when the kernel handles the death of the
562 	 * process, the file is not cleaned because unmap was not
563 	 * called, and the mm wouldn't be freed because we would still
564 	 * have a reference on mm_users. Incrementing mm_count solves
565 	 * the problem.
566 	 */
567 	if (mm)
568 		mmgrab(mm);
569 	trace_ocxl_context_add(current->pid, spa->spa_mem, pasid, pidr, tidr);
570 unlock:
571 	mutex_unlock(&spa->spa_lock);
572 	return rc;
573 }
574 EXPORT_SYMBOL_GPL(ocxl_link_add_pe);
575 
576 int ocxl_link_update_pe(void *link_handle, int pasid, __u16 tid)
577 {
578 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
579 	struct spa *spa = link->spa;
580 	struct ocxl_process_element *pe;
581 	int pe_handle, rc;
582 
583 	if (pasid > SPA_PASID_MAX)
584 		return -EINVAL;
585 
586 	pe_handle = pasid & SPA_PE_MASK;
587 	pe = spa->spa_mem + pe_handle;
588 
589 	mutex_lock(&spa->spa_lock);
590 
591 	pe->tid = cpu_to_be32(tid);
592 
593 	/*
594 	 * The barrier makes sure the PE is updated
595 	 * before we clear the NPU context cache below, so that the
596 	 * old PE cannot be reloaded erroneously.
597 	 */
598 	mb();
599 
600 	/*
601 	 * hook to platform code
602 	 * On powerpc, the entry needs to be cleared from the context
603 	 * cache of the NPU.
604 	 */
605 	rc = pnv_ocxl_spa_remove_pe_from_cache(link->platform_data, pe_handle);
606 	WARN_ON(rc);
607 
608 	mutex_unlock(&spa->spa_lock);
609 	return rc;
610 }
611 
612 int ocxl_link_remove_pe(void *link_handle, int pasid)
613 {
614 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
615 	struct spa *spa = link->spa;
616 	struct ocxl_process_element *pe;
617 	struct pe_data *pe_data;
618 	int pe_handle, rc;
619 
620 	if (pasid > SPA_PASID_MAX)
621 		return -EINVAL;
622 
623 	/*
624 	 * About synchronization with our memory fault handler:
625 	 *
626 	 * Before removing the PE, the driver is supposed to have
627 	 * notified the AFU, which should have cleaned up and make
628 	 * sure the PASID is no longer in use, including pending
629 	 * interrupts. However, there's no way to be sure...
630 	 *
631 	 * We clear the PE and remove the context from our radix
632 	 * tree. From that point on, any new interrupt for that
633 	 * context will fail silently, which is ok. As mentioned
634 	 * above, that's not expected, but it could happen if the
635 	 * driver or AFU didn't do the right thing.
636 	 *
637 	 * There could still be a bottom half running, but we don't
638 	 * need to wait/flush, as it is managing a reference count on
639 	 * the mm it reads from the radix tree.
640 	 */
641 	pe_handle = pasid & SPA_PE_MASK;
642 	pe = spa->spa_mem + pe_handle;
643 
644 	mutex_lock(&spa->spa_lock);
645 
646 	if (!(be32_to_cpu(pe->software_state) & SPA_PE_VALID)) {
647 		rc = -EINVAL;
648 		goto unlock;
649 	}
650 
651 	trace_ocxl_context_remove(current->pid, spa->spa_mem, pasid,
652 				be32_to_cpu(pe->pid), be32_to_cpu(pe->tid));
653 
654 	memset(pe, 0, sizeof(struct ocxl_process_element));
655 	/*
656 	 * The barrier makes sure the PE is removed from the SPA
657 	 * before we clear the NPU context cache below, so that the
658 	 * old PE cannot be reloaded erroneously.
659 	 */
660 	mb();
661 
662 	/*
663 	 * hook to platform code
664 	 * On powerpc, the entry needs to be cleared from the context
665 	 * cache of the NPU.
666 	 */
667 	rc = pnv_ocxl_spa_remove_pe_from_cache(link->platform_data, pe_handle);
668 	WARN_ON(rc);
669 
670 	pe_data = radix_tree_delete(&spa->pe_tree, pe_handle);
671 	if (!pe_data) {
672 		WARN(1, "Couldn't find pe data when removing PE\n");
673 	} else {
674 		if (pe_data->mm) {
675 			mm_context_remove_copro(pe_data->mm);
676 			mmdrop(pe_data->mm);
677 		}
678 		kfree_rcu(pe_data, rcu);
679 	}
680 unlock:
681 	mutex_unlock(&spa->spa_lock);
682 	return rc;
683 }
684 EXPORT_SYMBOL_GPL(ocxl_link_remove_pe);
685 
686 int ocxl_link_irq_alloc(void *link_handle, int *hw_irq)
687 {
688 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
689 	int irq;
690 
691 	if (atomic_dec_if_positive(&link->irq_available) < 0)
692 		return -ENOSPC;
693 
694 	irq = xive_native_alloc_irq();
695 	if (!irq) {
696 		atomic_inc(&link->irq_available);
697 		return -ENXIO;
698 	}
699 
700 	*hw_irq = irq;
701 	return 0;
702 }
703 EXPORT_SYMBOL_GPL(ocxl_link_irq_alloc);
704 
705 void ocxl_link_free_irq(void *link_handle, int hw_irq)
706 {
707 	struct ocxl_link *link = (struct ocxl_link *) link_handle;
708 
709 	xive_native_free_irq(hw_irq);
710 	atomic_inc(&link->irq_available);
711 }
712 EXPORT_SYMBOL_GPL(ocxl_link_free_irq);
713