xref: /openbmc/linux/drivers/gpu/drm/i915/gt/shmem_utils.c (revision d3402925)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include <linux/iosys-map.h>
7 #include <linux/mm.h>
8 #include <linux/pagemap.h>
9 #include <linux/shmem_fs.h>
10 
11 #include "i915_drv.h"
12 #include "gem/i915_gem_object.h"
13 #include "gem/i915_gem_lmem.h"
14 #include "shmem_utils.h"
15 
16 struct file *shmem_create_from_data(const char *name, void *data, size_t len)
17 {
18 	struct file *file;
19 	int err;
20 
21 	file = shmem_file_setup(name, PAGE_ALIGN(len), VM_NORESERVE);
22 	if (IS_ERR(file))
23 		return file;
24 
25 	err = shmem_write(file, 0, data, len);
26 	if (err) {
27 		fput(file);
28 		return ERR_PTR(err);
29 	}
30 
31 	return file;
32 }
33 
34 struct file *shmem_create_from_object(struct drm_i915_gem_object *obj)
35 {
36 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
37 	enum i915_map_type map_type;
38 	struct file *file;
39 	void *ptr;
40 
41 	if (i915_gem_object_is_shmem(obj)) {
42 		file = obj->base.filp;
43 		atomic_long_inc(&file->f_count);
44 		return file;
45 	}
46 
47 	map_type = i915_coherent_map_type(i915, obj, true);
48 	ptr = i915_gem_object_pin_map_unlocked(obj, map_type);
49 	if (IS_ERR(ptr))
50 		return ERR_CAST(ptr);
51 
52 	file = shmem_create_from_data("", ptr, obj->base.size);
53 	i915_gem_object_unpin_map(obj);
54 
55 	return file;
56 }
57 
58 void *shmem_pin_map(struct file *file)
59 {
60 	struct page **pages;
61 	size_t n_pages, i;
62 	void *vaddr;
63 
64 	n_pages = file->f_mapping->host->i_size >> PAGE_SHIFT;
65 	pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
66 	if (!pages)
67 		return NULL;
68 
69 	for (i = 0; i < n_pages; i++) {
70 		pages[i] = shmem_read_mapping_page_gfp(file->f_mapping, i,
71 						       GFP_KERNEL);
72 		if (IS_ERR(pages[i]))
73 			goto err_page;
74 	}
75 
76 	vaddr = vmap(pages, n_pages, VM_MAP_PUT_PAGES, PAGE_KERNEL);
77 	if (!vaddr)
78 		goto err_page;
79 	mapping_set_unevictable(file->f_mapping);
80 	return vaddr;
81 err_page:
82 	while (i--)
83 		put_page(pages[i]);
84 	kvfree(pages);
85 	return NULL;
86 }
87 
88 void shmem_unpin_map(struct file *file, void *ptr)
89 {
90 	mapping_clear_unevictable(file->f_mapping);
91 	vfree(ptr);
92 }
93 
94 static int __shmem_rw(struct file *file, loff_t off,
95 		      void *ptr, size_t len,
96 		      bool write)
97 {
98 	unsigned long pfn;
99 
100 	for (pfn = off >> PAGE_SHIFT; len; pfn++) {
101 		unsigned int this =
102 			min_t(size_t, PAGE_SIZE - offset_in_page(off), len);
103 		struct page *page;
104 		void *vaddr;
105 
106 		page = shmem_read_mapping_page_gfp(file->f_mapping, pfn,
107 						   GFP_KERNEL);
108 		if (IS_ERR(page))
109 			return PTR_ERR(page);
110 
111 		vaddr = kmap(page);
112 		if (write) {
113 			memcpy(vaddr + offset_in_page(off), ptr, this);
114 			set_page_dirty(page);
115 		} else {
116 			memcpy(ptr, vaddr + offset_in_page(off), this);
117 		}
118 		mark_page_accessed(page);
119 		kunmap(page);
120 		put_page(page);
121 
122 		len -= this;
123 		ptr += this;
124 		off = 0;
125 	}
126 
127 	return 0;
128 }
129 
130 int shmem_read_to_iosys_map(struct file *file, loff_t off,
131 			    struct iosys_map *map, size_t map_off, size_t len)
132 {
133 	unsigned long pfn;
134 
135 	for (pfn = off >> PAGE_SHIFT; len; pfn++) {
136 		unsigned int this =
137 			min_t(size_t, PAGE_SIZE - offset_in_page(off), len);
138 		struct page *page;
139 		void *vaddr;
140 
141 		page = shmem_read_mapping_page_gfp(file->f_mapping, pfn,
142 						   GFP_KERNEL);
143 		if (IS_ERR(page))
144 			return PTR_ERR(page);
145 
146 		vaddr = kmap(page);
147 		iosys_map_memcpy_to(map, map_off, vaddr + offset_in_page(off),
148 				    this);
149 		mark_page_accessed(page);
150 		kunmap(page);
151 		put_page(page);
152 
153 		len -= this;
154 		map_off += this;
155 		off = 0;
156 	}
157 
158 	return 0;
159 }
160 
161 int shmem_read(struct file *file, loff_t off, void *dst, size_t len)
162 {
163 	return __shmem_rw(file, off, dst, len, false);
164 }
165 
166 int shmem_write(struct file *file, loff_t off, void *src, size_t len)
167 {
168 	return __shmem_rw(file, off, src, len, true);
169 }
170 
171 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
172 #include "st_shmem_utils.c"
173 #endif
174