xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem.c (revision 7fc96d71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/dma-map-ops.h>
8 #include <linux/vmalloc.h>
9 #include <linux/spinlock.h>
10 #include <linux/shmem_fs.h>
11 #include <linux/dma-buf.h>
12 #include <linux/pfn_t.h>
13 
14 #include <drm/drm_prime.h>
15 
16 #include "msm_drv.h"
17 #include "msm_fence.h"
18 #include "msm_gem.h"
19 #include "msm_gpu.h"
20 #include "msm_mmu.h"
21 
22 static void update_inactive(struct msm_gem_object *msm_obj);
23 
24 static dma_addr_t physaddr(struct drm_gem_object *obj)
25 {
26 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
27 	struct msm_drm_private *priv = obj->dev->dev_private;
28 	return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) +
29 			priv->vram.paddr;
30 }
31 
32 static bool use_pages(struct drm_gem_object *obj)
33 {
34 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
35 	return !msm_obj->vram_node;
36 }
37 
38 /*
39  * Cache sync.. this is a bit over-complicated, to fit dma-mapping
40  * API.  Really GPU cache is out of scope here (handled on cmdstream)
41  * and all we need to do is invalidate newly allocated pages before
42  * mapping to CPU as uncached/writecombine.
43  *
44  * On top of this, we have the added headache, that depending on
45  * display generation, the display's iommu may be wired up to either
46  * the toplevel drm device (mdss), or to the mdp sub-node, meaning
47  * that here we either have dma-direct or iommu ops.
48  *
49  * Let this be a cautionary tail of abstraction gone wrong.
50  */
51 
52 static void sync_for_device(struct msm_gem_object *msm_obj)
53 {
54 	struct device *dev = msm_obj->base.dev->dev;
55 
56 	dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
57 }
58 
59 static void sync_for_cpu(struct msm_gem_object *msm_obj)
60 {
61 	struct device *dev = msm_obj->base.dev->dev;
62 
63 	dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0);
64 }
65 
66 /* allocate pages from VRAM carveout, used when no IOMMU: */
67 static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
68 {
69 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
70 	struct msm_drm_private *priv = obj->dev->dev_private;
71 	dma_addr_t paddr;
72 	struct page **p;
73 	int ret, i;
74 
75 	p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
76 	if (!p)
77 		return ERR_PTR(-ENOMEM);
78 
79 	spin_lock(&priv->vram.lock);
80 	ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
81 	spin_unlock(&priv->vram.lock);
82 	if (ret) {
83 		kvfree(p);
84 		return ERR_PTR(ret);
85 	}
86 
87 	paddr = physaddr(obj);
88 	for (i = 0; i < npages; i++) {
89 		p[i] = pfn_to_page(__phys_to_pfn(paddr));
90 		paddr += PAGE_SIZE;
91 	}
92 
93 	return p;
94 }
95 
96 static struct page **get_pages(struct drm_gem_object *obj)
97 {
98 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
99 
100 	GEM_WARN_ON(!msm_gem_is_locked(obj));
101 
102 	if (!msm_obj->pages) {
103 		struct drm_device *dev = obj->dev;
104 		struct page **p;
105 		int npages = obj->size >> PAGE_SHIFT;
106 
107 		if (use_pages(obj))
108 			p = drm_gem_get_pages(obj);
109 		else
110 			p = get_pages_vram(obj, npages);
111 
112 		if (IS_ERR(p)) {
113 			DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
114 					PTR_ERR(p));
115 			return p;
116 		}
117 
118 		msm_obj->pages = p;
119 
120 		msm_obj->sgt = drm_prime_pages_to_sg(obj->dev, p, npages);
121 		if (IS_ERR(msm_obj->sgt)) {
122 			void *ptr = ERR_CAST(msm_obj->sgt);
123 
124 			DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
125 			msm_obj->sgt = NULL;
126 			return ptr;
127 		}
128 
129 		/* For non-cached buffers, ensure the new pages are clean
130 		 * because display controller, GPU, etc. are not coherent:
131 		 */
132 		if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
133 			sync_for_device(msm_obj);
134 
135 		update_inactive(msm_obj);
136 	}
137 
138 	return msm_obj->pages;
139 }
140 
141 static void put_pages_vram(struct drm_gem_object *obj)
142 {
143 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
144 	struct msm_drm_private *priv = obj->dev->dev_private;
145 
146 	spin_lock(&priv->vram.lock);
147 	drm_mm_remove_node(msm_obj->vram_node);
148 	spin_unlock(&priv->vram.lock);
149 
150 	kvfree(msm_obj->pages);
151 }
152 
153 static void put_pages(struct drm_gem_object *obj)
154 {
155 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
156 
157 	if (msm_obj->pages) {
158 		if (msm_obj->sgt) {
159 			/* For non-cached buffers, ensure the new
160 			 * pages are clean because display controller,
161 			 * GPU, etc. are not coherent:
162 			 */
163 			if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
164 				sync_for_cpu(msm_obj);
165 
166 			sg_free_table(msm_obj->sgt);
167 			kfree(msm_obj->sgt);
168 			msm_obj->sgt = NULL;
169 		}
170 
171 		if (use_pages(obj))
172 			drm_gem_put_pages(obj, msm_obj->pages, true, false);
173 		else
174 			put_pages_vram(obj);
175 
176 		msm_obj->pages = NULL;
177 	}
178 }
179 
180 struct page **msm_gem_get_pages(struct drm_gem_object *obj)
181 {
182 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
183 	struct page **p;
184 
185 	msm_gem_lock(obj);
186 
187 	if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
188 		msm_gem_unlock(obj);
189 		return ERR_PTR(-EBUSY);
190 	}
191 
192 	p = get_pages(obj);
193 
194 	if (!IS_ERR(p)) {
195 		msm_obj->pin_count++;
196 		update_inactive(msm_obj);
197 	}
198 
199 	msm_gem_unlock(obj);
200 	return p;
201 }
202 
203 void msm_gem_put_pages(struct drm_gem_object *obj)
204 {
205 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
206 
207 	msm_gem_lock(obj);
208 	msm_obj->pin_count--;
209 	GEM_WARN_ON(msm_obj->pin_count < 0);
210 	update_inactive(msm_obj);
211 	msm_gem_unlock(obj);
212 }
213 
214 static pgprot_t msm_gem_pgprot(struct msm_gem_object *msm_obj, pgprot_t prot)
215 {
216 	if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
217 		return pgprot_writecombine(prot);
218 	return prot;
219 }
220 
221 static vm_fault_t msm_gem_fault(struct vm_fault *vmf)
222 {
223 	struct vm_area_struct *vma = vmf->vma;
224 	struct drm_gem_object *obj = vma->vm_private_data;
225 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
226 	struct page **pages;
227 	unsigned long pfn;
228 	pgoff_t pgoff;
229 	int err;
230 	vm_fault_t ret;
231 
232 	/*
233 	 * vm_ops.open/drm_gem_mmap_obj and close get and put
234 	 * a reference on obj. So, we dont need to hold one here.
235 	 */
236 	err = msm_gem_lock_interruptible(obj);
237 	if (err) {
238 		ret = VM_FAULT_NOPAGE;
239 		goto out;
240 	}
241 
242 	if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
243 		msm_gem_unlock(obj);
244 		return VM_FAULT_SIGBUS;
245 	}
246 
247 	/* make sure we have pages attached now */
248 	pages = get_pages(obj);
249 	if (IS_ERR(pages)) {
250 		ret = vmf_error(PTR_ERR(pages));
251 		goto out_unlock;
252 	}
253 
254 	/* We don't use vmf->pgoff since that has the fake offset: */
255 	pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
256 
257 	pfn = page_to_pfn(pages[pgoff]);
258 
259 	VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
260 			pfn, pfn << PAGE_SHIFT);
261 
262 	ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
263 out_unlock:
264 	msm_gem_unlock(obj);
265 out:
266 	return ret;
267 }
268 
269 /** get mmap offset */
270 static uint64_t mmap_offset(struct drm_gem_object *obj)
271 {
272 	struct drm_device *dev = obj->dev;
273 	int ret;
274 
275 	GEM_WARN_ON(!msm_gem_is_locked(obj));
276 
277 	/* Make it mmapable */
278 	ret = drm_gem_create_mmap_offset(obj);
279 
280 	if (ret) {
281 		DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
282 		return 0;
283 	}
284 
285 	return drm_vma_node_offset_addr(&obj->vma_node);
286 }
287 
288 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
289 {
290 	uint64_t offset;
291 
292 	msm_gem_lock(obj);
293 	offset = mmap_offset(obj);
294 	msm_gem_unlock(obj);
295 	return offset;
296 }
297 
298 static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
299 		struct msm_gem_address_space *aspace)
300 {
301 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
302 	struct msm_gem_vma *vma;
303 
304 	GEM_WARN_ON(!msm_gem_is_locked(obj));
305 
306 	vma = kzalloc(sizeof(*vma), GFP_KERNEL);
307 	if (!vma)
308 		return ERR_PTR(-ENOMEM);
309 
310 	vma->aspace = aspace;
311 
312 	list_add_tail(&vma->list, &msm_obj->vmas);
313 
314 	return vma;
315 }
316 
317 static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
318 		struct msm_gem_address_space *aspace)
319 {
320 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
321 	struct msm_gem_vma *vma;
322 
323 	GEM_WARN_ON(!msm_gem_is_locked(obj));
324 
325 	list_for_each_entry(vma, &msm_obj->vmas, list) {
326 		if (vma->aspace == aspace)
327 			return vma;
328 	}
329 
330 	return NULL;
331 }
332 
333 static void del_vma(struct msm_gem_vma *vma)
334 {
335 	if (!vma)
336 		return;
337 
338 	list_del(&vma->list);
339 	kfree(vma);
340 }
341 
342 /*
343  * If close is true, this also closes the VMA (releasing the allocated
344  * iova range) in addition to removing the iommu mapping.  In the eviction
345  * case (!close), we keep the iova allocated, but only remove the iommu
346  * mapping.
347  */
348 static void
349 put_iova_spaces(struct drm_gem_object *obj, bool close)
350 {
351 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
352 	struct msm_gem_vma *vma;
353 
354 	GEM_WARN_ON(!msm_gem_is_locked(obj));
355 
356 	list_for_each_entry(vma, &msm_obj->vmas, list) {
357 		if (vma->aspace) {
358 			msm_gem_purge_vma(vma->aspace, vma);
359 			if (close)
360 				msm_gem_close_vma(vma->aspace, vma);
361 		}
362 	}
363 }
364 
365 /* Called with msm_obj locked */
366 static void
367 put_iova_vmas(struct drm_gem_object *obj)
368 {
369 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
370 	struct msm_gem_vma *vma, *tmp;
371 
372 	GEM_WARN_ON(!msm_gem_is_locked(obj));
373 
374 	list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
375 		del_vma(vma);
376 	}
377 }
378 
379 static struct msm_gem_vma *get_vma_locked(struct drm_gem_object *obj,
380 		struct msm_gem_address_space *aspace,
381 		u64 range_start, u64 range_end)
382 {
383 	struct msm_gem_vma *vma;
384 
385 	GEM_WARN_ON(!msm_gem_is_locked(obj));
386 
387 	vma = lookup_vma(obj, aspace);
388 
389 	if (!vma) {
390 		int ret;
391 
392 		vma = add_vma(obj, aspace);
393 		if (IS_ERR(vma))
394 			return vma;
395 
396 		ret = msm_gem_init_vma(aspace, vma, obj->size,
397 			range_start, range_end);
398 		if (ret) {
399 			del_vma(vma);
400 			return ERR_PTR(ret);
401 		}
402 	} else {
403 		GEM_WARN_ON(vma->iova < range_start);
404 		GEM_WARN_ON((vma->iova + obj->size) > range_end);
405 	}
406 
407 	return vma;
408 }
409 
410 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma)
411 {
412 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
413 	struct page **pages;
414 	int ret, prot = IOMMU_READ;
415 
416 	if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
417 		prot |= IOMMU_WRITE;
418 
419 	if (msm_obj->flags & MSM_BO_MAP_PRIV)
420 		prot |= IOMMU_PRIV;
421 
422 	if (msm_obj->flags & MSM_BO_CACHED_COHERENT)
423 		prot |= IOMMU_CACHE;
424 
425 	GEM_WARN_ON(!msm_gem_is_locked(obj));
426 
427 	if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
428 		return -EBUSY;
429 
430 	pages = get_pages(obj);
431 	if (IS_ERR(pages))
432 		return PTR_ERR(pages);
433 
434 	ret = msm_gem_map_vma(vma->aspace, vma, prot, msm_obj->sgt, obj->size);
435 
436 	if (!ret)
437 		msm_obj->pin_count++;
438 
439 	return ret;
440 }
441 
442 void msm_gem_unpin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma)
443 {
444 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
445 
446 	GEM_WARN_ON(!msm_gem_is_locked(obj));
447 
448 	msm_gem_unpin_vma(vma);
449 
450 	msm_obj->pin_count--;
451 	GEM_WARN_ON(msm_obj->pin_count < 0);
452 
453 	update_inactive(msm_obj);
454 }
455 
456 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
457 					   struct msm_gem_address_space *aspace)
458 {
459 	return get_vma_locked(obj, aspace, 0, U64_MAX);
460 }
461 
462 static int get_and_pin_iova_range_locked(struct drm_gem_object *obj,
463 		struct msm_gem_address_space *aspace, uint64_t *iova,
464 		u64 range_start, u64 range_end)
465 {
466 	struct msm_gem_vma *vma;
467 	int ret;
468 
469 	GEM_WARN_ON(!msm_gem_is_locked(obj));
470 
471 	vma = get_vma_locked(obj, aspace, range_start, range_end);
472 	if (IS_ERR(vma))
473 		return PTR_ERR(vma);
474 
475 	ret = msm_gem_pin_vma_locked(obj, vma);
476 	if (!ret)
477 		*iova = vma->iova;
478 
479 	return ret;
480 }
481 
482 /*
483  * get iova and pin it. Should have a matching put
484  * limits iova to specified range (in pages)
485  */
486 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
487 		struct msm_gem_address_space *aspace, uint64_t *iova,
488 		u64 range_start, u64 range_end)
489 {
490 	int ret;
491 
492 	msm_gem_lock(obj);
493 	ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end);
494 	msm_gem_unlock(obj);
495 
496 	return ret;
497 }
498 
499 /* get iova and pin it. Should have a matching put */
500 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
501 		struct msm_gem_address_space *aspace, uint64_t *iova)
502 {
503 	return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX);
504 }
505 
506 /*
507  * Get an iova but don't pin it. Doesn't need a put because iovas are currently
508  * valid for the life of the object
509  */
510 int msm_gem_get_iova(struct drm_gem_object *obj,
511 		struct msm_gem_address_space *aspace, uint64_t *iova)
512 {
513 	struct msm_gem_vma *vma;
514 	int ret = 0;
515 
516 	msm_gem_lock(obj);
517 	vma = get_vma_locked(obj, aspace, 0, U64_MAX);
518 	if (IS_ERR(vma)) {
519 		ret = PTR_ERR(vma);
520 	} else {
521 		*iova = vma->iova;
522 	}
523 	msm_gem_unlock(obj);
524 
525 	return ret;
526 }
527 
528 static int clear_iova(struct drm_gem_object *obj,
529 		      struct msm_gem_address_space *aspace)
530 {
531 	struct msm_gem_vma *vma = lookup_vma(obj, aspace);
532 
533 	if (!vma)
534 		return 0;
535 
536 	if (msm_gem_vma_inuse(vma))
537 		return -EBUSY;
538 
539 	msm_gem_purge_vma(vma->aspace, vma);
540 	msm_gem_close_vma(vma->aspace, vma);
541 	del_vma(vma);
542 
543 	return 0;
544 }
545 
546 /*
547  * Get the requested iova but don't pin it.  Fails if the requested iova is
548  * not available.  Doesn't need a put because iovas are currently valid for
549  * the life of the object.
550  *
551  * Setting an iova of zero will clear the vma.
552  */
553 int msm_gem_set_iova(struct drm_gem_object *obj,
554 		     struct msm_gem_address_space *aspace, uint64_t iova)
555 {
556 	int ret = 0;
557 
558 	msm_gem_lock(obj);
559 	if (!iova) {
560 		ret = clear_iova(obj, aspace);
561 	} else {
562 		struct msm_gem_vma *vma;
563 		vma = get_vma_locked(obj, aspace, iova, iova + obj->size);
564 		if (IS_ERR(vma)) {
565 			ret = PTR_ERR(vma);
566 		} else if (GEM_WARN_ON(vma->iova != iova)) {
567 			clear_iova(obj, aspace);
568 			ret = -EBUSY;
569 		}
570 	}
571 	msm_gem_unlock(obj);
572 
573 	return ret;
574 }
575 
576 /*
577  * Unpin a iova by updating the reference counts. The memory isn't actually
578  * purged until something else (shrinker, mm_notifier, destroy, etc) decides
579  * to get rid of it
580  */
581 void msm_gem_unpin_iova(struct drm_gem_object *obj,
582 		struct msm_gem_address_space *aspace)
583 {
584 	struct msm_gem_vma *vma;
585 
586 	msm_gem_lock(obj);
587 	vma = lookup_vma(obj, aspace);
588 	if (!GEM_WARN_ON(!vma)) {
589 		msm_gem_unpin_vma_locked(obj, vma);
590 	}
591 	msm_gem_unlock(obj);
592 }
593 
594 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
595 		struct drm_mode_create_dumb *args)
596 {
597 	args->pitch = align_pitch(args->width, args->bpp);
598 	args->size  = PAGE_ALIGN(args->pitch * args->height);
599 	return msm_gem_new_handle(dev, file, args->size,
600 			MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
601 }
602 
603 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
604 		uint32_t handle, uint64_t *offset)
605 {
606 	struct drm_gem_object *obj;
607 	int ret = 0;
608 
609 	/* GEM does all our handle to object mapping */
610 	obj = drm_gem_object_lookup(file, handle);
611 	if (obj == NULL) {
612 		ret = -ENOENT;
613 		goto fail;
614 	}
615 
616 	*offset = msm_gem_mmap_offset(obj);
617 
618 	drm_gem_object_put(obj);
619 
620 fail:
621 	return ret;
622 }
623 
624 static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
625 {
626 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
627 	int ret = 0;
628 
629 	GEM_WARN_ON(!msm_gem_is_locked(obj));
630 
631 	if (obj->import_attach)
632 		return ERR_PTR(-ENODEV);
633 
634 	if (GEM_WARN_ON(msm_obj->madv > madv)) {
635 		DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
636 			msm_obj->madv, madv);
637 		return ERR_PTR(-EBUSY);
638 	}
639 
640 	/* increment vmap_count *before* vmap() call, so shrinker can
641 	 * check vmap_count (is_vunmapable()) outside of msm_obj lock.
642 	 * This guarantees that we won't try to msm_gem_vunmap() this
643 	 * same object from within the vmap() call (while we already
644 	 * hold msm_obj lock)
645 	 */
646 	msm_obj->vmap_count++;
647 
648 	if (!msm_obj->vaddr) {
649 		struct page **pages = get_pages(obj);
650 		if (IS_ERR(pages)) {
651 			ret = PTR_ERR(pages);
652 			goto fail;
653 		}
654 		msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
655 				VM_MAP, msm_gem_pgprot(msm_obj, PAGE_KERNEL));
656 		if (msm_obj->vaddr == NULL) {
657 			ret = -ENOMEM;
658 			goto fail;
659 		}
660 
661 		update_inactive(msm_obj);
662 	}
663 
664 	return msm_obj->vaddr;
665 
666 fail:
667 	msm_obj->vmap_count--;
668 	return ERR_PTR(ret);
669 }
670 
671 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj)
672 {
673 	return get_vaddr(obj, MSM_MADV_WILLNEED);
674 }
675 
676 void *msm_gem_get_vaddr(struct drm_gem_object *obj)
677 {
678 	void *ret;
679 
680 	msm_gem_lock(obj);
681 	ret = msm_gem_get_vaddr_locked(obj);
682 	msm_gem_unlock(obj);
683 
684 	return ret;
685 }
686 
687 /*
688  * Don't use this!  It is for the very special case of dumping
689  * submits from GPU hangs or faults, were the bo may already
690  * be MSM_MADV_DONTNEED, but we know the buffer is still on the
691  * active list.
692  */
693 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
694 {
695 	return get_vaddr(obj, __MSM_MADV_PURGED);
696 }
697 
698 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj)
699 {
700 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
701 
702 	GEM_WARN_ON(!msm_gem_is_locked(obj));
703 	GEM_WARN_ON(msm_obj->vmap_count < 1);
704 
705 	msm_obj->vmap_count--;
706 }
707 
708 void msm_gem_put_vaddr(struct drm_gem_object *obj)
709 {
710 	msm_gem_lock(obj);
711 	msm_gem_put_vaddr_locked(obj);
712 	msm_gem_unlock(obj);
713 }
714 
715 /* Update madvise status, returns true if not purged, else
716  * false or -errno.
717  */
718 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
719 {
720 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
721 
722 	msm_gem_lock(obj);
723 
724 	if (msm_obj->madv != __MSM_MADV_PURGED)
725 		msm_obj->madv = madv;
726 
727 	madv = msm_obj->madv;
728 
729 	/* If the obj is inactive, we might need to move it
730 	 * between inactive lists
731 	 */
732 	if (msm_obj->active_count == 0)
733 		update_inactive(msm_obj);
734 
735 	msm_gem_unlock(obj);
736 
737 	return (madv != __MSM_MADV_PURGED);
738 }
739 
740 void msm_gem_purge(struct drm_gem_object *obj)
741 {
742 	struct drm_device *dev = obj->dev;
743 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
744 
745 	GEM_WARN_ON(!msm_gem_is_locked(obj));
746 	GEM_WARN_ON(!is_purgeable(msm_obj));
747 
748 	/* Get rid of any iommu mapping(s): */
749 	put_iova_spaces(obj, true);
750 
751 	msm_gem_vunmap(obj);
752 
753 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
754 
755 	put_pages(obj);
756 
757 	put_iova_vmas(obj);
758 
759 	msm_obj->madv = __MSM_MADV_PURGED;
760 	update_inactive(msm_obj);
761 
762 	drm_gem_free_mmap_offset(obj);
763 
764 	/* Our goal here is to return as much of the memory as
765 	 * is possible back to the system as we are called from OOM.
766 	 * To do this we must instruct the shmfs to drop all of its
767 	 * backing pages, *now*.
768 	 */
769 	shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
770 
771 	invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
772 			0, (loff_t)-1);
773 }
774 
775 /*
776  * Unpin the backing pages and make them available to be swapped out.
777  */
778 void msm_gem_evict(struct drm_gem_object *obj)
779 {
780 	struct drm_device *dev = obj->dev;
781 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
782 
783 	GEM_WARN_ON(!msm_gem_is_locked(obj));
784 	GEM_WARN_ON(is_unevictable(msm_obj));
785 	GEM_WARN_ON(!msm_obj->evictable);
786 	GEM_WARN_ON(msm_obj->active_count);
787 
788 	/* Get rid of any iommu mapping(s): */
789 	put_iova_spaces(obj, false);
790 
791 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
792 
793 	put_pages(obj);
794 
795 	update_inactive(msm_obj);
796 }
797 
798 void msm_gem_vunmap(struct drm_gem_object *obj)
799 {
800 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
801 
802 	GEM_WARN_ON(!msm_gem_is_locked(obj));
803 
804 	if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj)))
805 		return;
806 
807 	vunmap(msm_obj->vaddr);
808 	msm_obj->vaddr = NULL;
809 }
810 
811 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu)
812 {
813 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
814 	struct msm_drm_private *priv = obj->dev->dev_private;
815 
816 	might_sleep();
817 	GEM_WARN_ON(!msm_gem_is_locked(obj));
818 	GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
819 	GEM_WARN_ON(msm_obj->dontneed);
820 
821 	if (msm_obj->active_count++ == 0) {
822 		mutex_lock(&priv->mm_lock);
823 		if (msm_obj->evictable)
824 			mark_unevictable(msm_obj);
825 		list_move_tail(&msm_obj->mm_list, &gpu->active_list);
826 		mutex_unlock(&priv->mm_lock);
827 	}
828 }
829 
830 void msm_gem_active_put(struct drm_gem_object *obj)
831 {
832 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
833 
834 	might_sleep();
835 	GEM_WARN_ON(!msm_gem_is_locked(obj));
836 
837 	if (--msm_obj->active_count == 0) {
838 		update_inactive(msm_obj);
839 	}
840 }
841 
842 static void update_inactive(struct msm_gem_object *msm_obj)
843 {
844 	struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
845 
846 	GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base));
847 
848 	if (msm_obj->active_count != 0)
849 		return;
850 
851 	mutex_lock(&priv->mm_lock);
852 
853 	if (msm_obj->dontneed)
854 		mark_unpurgeable(msm_obj);
855 	if (msm_obj->evictable)
856 		mark_unevictable(msm_obj);
857 
858 	list_del(&msm_obj->mm_list);
859 	if ((msm_obj->madv == MSM_MADV_WILLNEED) && msm_obj->sgt) {
860 		list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed);
861 		mark_evictable(msm_obj);
862 	} else if (msm_obj->madv == MSM_MADV_DONTNEED) {
863 		list_add_tail(&msm_obj->mm_list, &priv->inactive_dontneed);
864 		mark_purgeable(msm_obj);
865 	} else {
866 		GEM_WARN_ON((msm_obj->madv != __MSM_MADV_PURGED) && msm_obj->sgt);
867 		list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
868 	}
869 
870 	mutex_unlock(&priv->mm_lock);
871 }
872 
873 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
874 {
875 	bool write = !!(op & MSM_PREP_WRITE);
876 	unsigned long remain =
877 		op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
878 	long ret;
879 
880 	ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(write),
881 				    true,  remain);
882 	if (ret == 0)
883 		return remain == 0 ? -EBUSY : -ETIMEDOUT;
884 	else if (ret < 0)
885 		return ret;
886 
887 	/* TODO cache maintenance */
888 
889 	return 0;
890 }
891 
892 int msm_gem_cpu_fini(struct drm_gem_object *obj)
893 {
894 	/* TODO cache maintenance */
895 	return 0;
896 }
897 
898 #ifdef CONFIG_DEBUG_FS
899 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
900 		struct msm_gem_stats *stats)
901 {
902 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
903 	struct dma_resv *robj = obj->resv;
904 	struct msm_gem_vma *vma;
905 	uint64_t off = drm_vma_node_start(&obj->vma_node);
906 	const char *madv;
907 
908 	msm_gem_lock(obj);
909 
910 	stats->all.count++;
911 	stats->all.size += obj->size;
912 
913 	if (is_active(msm_obj)) {
914 		stats->active.count++;
915 		stats->active.size += obj->size;
916 	}
917 
918 	if (msm_obj->pages) {
919 		stats->resident.count++;
920 		stats->resident.size += obj->size;
921 	}
922 
923 	switch (msm_obj->madv) {
924 	case __MSM_MADV_PURGED:
925 		stats->purged.count++;
926 		stats->purged.size += obj->size;
927 		madv = " purged";
928 		break;
929 	case MSM_MADV_DONTNEED:
930 		stats->purgeable.count++;
931 		stats->purgeable.size += obj->size;
932 		madv = " purgeable";
933 		break;
934 	case MSM_MADV_WILLNEED:
935 	default:
936 		madv = "";
937 		break;
938 	}
939 
940 	seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
941 			msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
942 			obj->name, kref_read(&obj->refcount),
943 			off, msm_obj->vaddr);
944 
945 	seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
946 
947 	if (!list_empty(&msm_obj->vmas)) {
948 
949 		seq_puts(m, "      vmas:");
950 
951 		list_for_each_entry(vma, &msm_obj->vmas, list) {
952 			const char *name, *comm;
953 			if (vma->aspace) {
954 				struct msm_gem_address_space *aspace = vma->aspace;
955 				struct task_struct *task =
956 					get_pid_task(aspace->pid, PIDTYPE_PID);
957 				if (task) {
958 					comm = kstrdup(task->comm, GFP_KERNEL);
959 					put_task_struct(task);
960 				} else {
961 					comm = NULL;
962 				}
963 				name = aspace->name;
964 			} else {
965 				name = comm = NULL;
966 			}
967 			seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]",
968 				name, comm ? ":" : "", comm ? comm : "",
969 				vma->aspace, vma->iova,
970 				vma->mapped ? "mapped" : "unmapped",
971 				msm_gem_vma_inuse(vma));
972 			kfree(comm);
973 		}
974 
975 		seq_puts(m, "\n");
976 	}
977 
978 	dma_resv_describe(robj, m);
979 	msm_gem_unlock(obj);
980 }
981 
982 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
983 {
984 	struct msm_gem_stats stats = {};
985 	struct msm_gem_object *msm_obj;
986 
987 	seq_puts(m, "   flags       id ref  offset   kaddr            size     madv      name\n");
988 	list_for_each_entry(msm_obj, list, node) {
989 		struct drm_gem_object *obj = &msm_obj->base;
990 		seq_puts(m, "   ");
991 		msm_gem_describe(obj, m, &stats);
992 	}
993 
994 	seq_printf(m, "Total:     %4d objects, %9zu bytes\n",
995 			stats.all.count, stats.all.size);
996 	seq_printf(m, "Active:    %4d objects, %9zu bytes\n",
997 			stats.active.count, stats.active.size);
998 	seq_printf(m, "Resident:  %4d objects, %9zu bytes\n",
999 			stats.resident.count, stats.resident.size);
1000 	seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n",
1001 			stats.purgeable.count, stats.purgeable.size);
1002 	seq_printf(m, "Purged:    %4d objects, %9zu bytes\n",
1003 			stats.purged.count, stats.purged.size);
1004 }
1005 #endif
1006 
1007 /* don't call directly!  Use drm_gem_object_put() */
1008 void msm_gem_free_object(struct drm_gem_object *obj)
1009 {
1010 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
1011 	struct drm_device *dev = obj->dev;
1012 	struct msm_drm_private *priv = dev->dev_private;
1013 
1014 	mutex_lock(&priv->obj_lock);
1015 	list_del(&msm_obj->node);
1016 	mutex_unlock(&priv->obj_lock);
1017 
1018 	mutex_lock(&priv->mm_lock);
1019 	if (msm_obj->dontneed)
1020 		mark_unpurgeable(msm_obj);
1021 	list_del(&msm_obj->mm_list);
1022 	mutex_unlock(&priv->mm_lock);
1023 
1024 	msm_gem_lock(obj);
1025 
1026 	/* object should not be on active list: */
1027 	GEM_WARN_ON(is_active(msm_obj));
1028 
1029 	put_iova_spaces(obj, true);
1030 
1031 	if (obj->import_attach) {
1032 		GEM_WARN_ON(msm_obj->vaddr);
1033 
1034 		/* Don't drop the pages for imported dmabuf, as they are not
1035 		 * ours, just free the array we allocated:
1036 		 */
1037 		kvfree(msm_obj->pages);
1038 
1039 		put_iova_vmas(obj);
1040 
1041 		/* dma_buf_detach() grabs resv lock, so we need to unlock
1042 		 * prior to drm_prime_gem_destroy
1043 		 */
1044 		msm_gem_unlock(obj);
1045 
1046 		drm_prime_gem_destroy(obj, msm_obj->sgt);
1047 	} else {
1048 		msm_gem_vunmap(obj);
1049 		put_pages(obj);
1050 		put_iova_vmas(obj);
1051 		msm_gem_unlock(obj);
1052 	}
1053 
1054 	drm_gem_object_release(obj);
1055 
1056 	kfree(msm_obj);
1057 }
1058 
1059 static int msm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
1060 {
1061 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
1062 
1063 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
1064 	vma->vm_page_prot = msm_gem_pgprot(msm_obj, vm_get_page_prot(vma->vm_flags));
1065 
1066 	return 0;
1067 }
1068 
1069 /* convenience method to construct a GEM buffer object, and userspace handle */
1070 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
1071 		uint32_t size, uint32_t flags, uint32_t *handle,
1072 		char *name)
1073 {
1074 	struct drm_gem_object *obj;
1075 	int ret;
1076 
1077 	obj = msm_gem_new(dev, size, flags);
1078 
1079 	if (IS_ERR(obj))
1080 		return PTR_ERR(obj);
1081 
1082 	if (name)
1083 		msm_gem_object_set_name(obj, "%s", name);
1084 
1085 	ret = drm_gem_handle_create(file, obj, handle);
1086 
1087 	/* drop reference from allocate - handle holds it now */
1088 	drm_gem_object_put(obj);
1089 
1090 	return ret;
1091 }
1092 
1093 static const struct vm_operations_struct vm_ops = {
1094 	.fault = msm_gem_fault,
1095 	.open = drm_gem_vm_open,
1096 	.close = drm_gem_vm_close,
1097 };
1098 
1099 static const struct drm_gem_object_funcs msm_gem_object_funcs = {
1100 	.free = msm_gem_free_object,
1101 	.pin = msm_gem_prime_pin,
1102 	.unpin = msm_gem_prime_unpin,
1103 	.get_sg_table = msm_gem_prime_get_sg_table,
1104 	.vmap = msm_gem_prime_vmap,
1105 	.vunmap = msm_gem_prime_vunmap,
1106 	.mmap = msm_gem_object_mmap,
1107 	.vm_ops = &vm_ops,
1108 };
1109 
1110 static int msm_gem_new_impl(struct drm_device *dev,
1111 		uint32_t size, uint32_t flags,
1112 		struct drm_gem_object **obj)
1113 {
1114 	struct msm_drm_private *priv = dev->dev_private;
1115 	struct msm_gem_object *msm_obj;
1116 
1117 	switch (flags & MSM_BO_CACHE_MASK) {
1118 	case MSM_BO_UNCACHED:
1119 	case MSM_BO_CACHED:
1120 	case MSM_BO_WC:
1121 		break;
1122 	case MSM_BO_CACHED_COHERENT:
1123 		if (priv->has_cached_coherent)
1124 			break;
1125 		fallthrough;
1126 	default:
1127 		DRM_DEV_DEBUG(dev->dev, "invalid cache flag: %x\n",
1128 				(flags & MSM_BO_CACHE_MASK));
1129 		return -EINVAL;
1130 	}
1131 
1132 	msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
1133 	if (!msm_obj)
1134 		return -ENOMEM;
1135 
1136 	msm_obj->flags = flags;
1137 	msm_obj->madv = MSM_MADV_WILLNEED;
1138 
1139 	INIT_LIST_HEAD(&msm_obj->node);
1140 	INIT_LIST_HEAD(&msm_obj->vmas);
1141 
1142 	*obj = &msm_obj->base;
1143 	(*obj)->funcs = &msm_gem_object_funcs;
1144 
1145 	return 0;
1146 }
1147 
1148 struct drm_gem_object *msm_gem_new(struct drm_device *dev, uint32_t size, uint32_t flags)
1149 {
1150 	struct msm_drm_private *priv = dev->dev_private;
1151 	struct msm_gem_object *msm_obj;
1152 	struct drm_gem_object *obj = NULL;
1153 	bool use_vram = false;
1154 	int ret;
1155 
1156 	size = PAGE_ALIGN(size);
1157 
1158 	if (!msm_use_mmu(dev))
1159 		use_vram = true;
1160 	else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
1161 		use_vram = true;
1162 
1163 	if (GEM_WARN_ON(use_vram && !priv->vram.size))
1164 		return ERR_PTR(-EINVAL);
1165 
1166 	/* Disallow zero sized objects as they make the underlying
1167 	 * infrastructure grumpy
1168 	 */
1169 	if (size == 0)
1170 		return ERR_PTR(-EINVAL);
1171 
1172 	ret = msm_gem_new_impl(dev, size, flags, &obj);
1173 	if (ret)
1174 		return ERR_PTR(ret);
1175 
1176 	msm_obj = to_msm_bo(obj);
1177 
1178 	if (use_vram) {
1179 		struct msm_gem_vma *vma;
1180 		struct page **pages;
1181 
1182 		drm_gem_private_object_init(dev, obj, size);
1183 
1184 		msm_gem_lock(obj);
1185 
1186 		vma = add_vma(obj, NULL);
1187 		msm_gem_unlock(obj);
1188 		if (IS_ERR(vma)) {
1189 			ret = PTR_ERR(vma);
1190 			goto fail;
1191 		}
1192 
1193 		to_msm_bo(obj)->vram_node = &vma->node;
1194 
1195 		/* Call chain get_pages() -> update_inactive() tries to
1196 		 * access msm_obj->mm_list, but it is not initialized yet.
1197 		 * To avoid NULL pointer dereference error, initialize
1198 		 * mm_list to be empty.
1199 		 */
1200 		INIT_LIST_HEAD(&msm_obj->mm_list);
1201 
1202 		msm_gem_lock(obj);
1203 		pages = get_pages(obj);
1204 		msm_gem_unlock(obj);
1205 		if (IS_ERR(pages)) {
1206 			ret = PTR_ERR(pages);
1207 			goto fail;
1208 		}
1209 
1210 		vma->iova = physaddr(obj);
1211 	} else {
1212 		ret = drm_gem_object_init(dev, obj, size);
1213 		if (ret)
1214 			goto fail;
1215 		/*
1216 		 * Our buffers are kept pinned, so allocating them from the
1217 		 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1218 		 * See comments above new_inode() why this is required _and_
1219 		 * expected if you're going to pin these pages.
1220 		 */
1221 		mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
1222 	}
1223 
1224 	mutex_lock(&priv->mm_lock);
1225 	list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1226 	mutex_unlock(&priv->mm_lock);
1227 
1228 	mutex_lock(&priv->obj_lock);
1229 	list_add_tail(&msm_obj->node, &priv->objects);
1230 	mutex_unlock(&priv->obj_lock);
1231 
1232 	return obj;
1233 
1234 fail:
1235 	drm_gem_object_put(obj);
1236 	return ERR_PTR(ret);
1237 }
1238 
1239 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
1240 		struct dma_buf *dmabuf, struct sg_table *sgt)
1241 {
1242 	struct msm_drm_private *priv = dev->dev_private;
1243 	struct msm_gem_object *msm_obj;
1244 	struct drm_gem_object *obj;
1245 	uint32_t size;
1246 	int ret, npages;
1247 
1248 	/* if we don't have IOMMU, don't bother pretending we can import: */
1249 	if (!msm_use_mmu(dev)) {
1250 		DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
1251 		return ERR_PTR(-EINVAL);
1252 	}
1253 
1254 	size = PAGE_ALIGN(dmabuf->size);
1255 
1256 	ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj);
1257 	if (ret)
1258 		return ERR_PTR(ret);
1259 
1260 	drm_gem_private_object_init(dev, obj, size);
1261 
1262 	npages = size / PAGE_SIZE;
1263 
1264 	msm_obj = to_msm_bo(obj);
1265 	msm_gem_lock(obj);
1266 	msm_obj->sgt = sgt;
1267 	msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1268 	if (!msm_obj->pages) {
1269 		msm_gem_unlock(obj);
1270 		ret = -ENOMEM;
1271 		goto fail;
1272 	}
1273 
1274 	ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages);
1275 	if (ret) {
1276 		msm_gem_unlock(obj);
1277 		goto fail;
1278 	}
1279 
1280 	msm_gem_unlock(obj);
1281 
1282 	mutex_lock(&priv->mm_lock);
1283 	list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned);
1284 	mutex_unlock(&priv->mm_lock);
1285 
1286 	mutex_lock(&priv->obj_lock);
1287 	list_add_tail(&msm_obj->node, &priv->objects);
1288 	mutex_unlock(&priv->obj_lock);
1289 
1290 	return obj;
1291 
1292 fail:
1293 	drm_gem_object_put(obj);
1294 	return ERR_PTR(ret);
1295 }
1296 
1297 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1298 		uint32_t flags, struct msm_gem_address_space *aspace,
1299 		struct drm_gem_object **bo, uint64_t *iova)
1300 {
1301 	void *vaddr;
1302 	struct drm_gem_object *obj = msm_gem_new(dev, size, flags);
1303 	int ret;
1304 
1305 	if (IS_ERR(obj))
1306 		return ERR_CAST(obj);
1307 
1308 	if (iova) {
1309 		ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
1310 		if (ret)
1311 			goto err;
1312 	}
1313 
1314 	vaddr = msm_gem_get_vaddr(obj);
1315 	if (IS_ERR(vaddr)) {
1316 		msm_gem_unpin_iova(obj, aspace);
1317 		ret = PTR_ERR(vaddr);
1318 		goto err;
1319 	}
1320 
1321 	if (bo)
1322 		*bo = obj;
1323 
1324 	return vaddr;
1325 err:
1326 	drm_gem_object_put(obj);
1327 
1328 	return ERR_PTR(ret);
1329 
1330 }
1331 
1332 void msm_gem_kernel_put(struct drm_gem_object *bo,
1333 		struct msm_gem_address_space *aspace)
1334 {
1335 	if (IS_ERR_OR_NULL(bo))
1336 		return;
1337 
1338 	msm_gem_put_vaddr(bo);
1339 	msm_gem_unpin_iova(bo, aspace);
1340 	drm_gem_object_put(bo);
1341 }
1342 
1343 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1344 {
1345 	struct msm_gem_object *msm_obj = to_msm_bo(bo);
1346 	va_list ap;
1347 
1348 	if (!fmt)
1349 		return;
1350 
1351 	va_start(ap, fmt);
1352 	vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);
1353 	va_end(ap);
1354 }
1355