Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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