xref: /openbmc/linux/drivers/gpu/drm/i915/gem/i915_gem_lmem.c (revision b0e55fef624e511e060fa05e4ca96cae6d902f04)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "intel_memory_region.h"
7 #include "gem/i915_gem_region.h"
8 #include "gem/i915_gem_lmem.h"
9 #include "i915_drv.h"
10 
11 const struct drm_i915_gem_object_ops i915_gem_lmem_obj_ops = {
12 	.flags = I915_GEM_OBJECT_HAS_IOMEM,
13 
14 	.get_pages = i915_gem_object_get_pages_buddy,
15 	.put_pages = i915_gem_object_put_pages_buddy,
16 	.release = i915_gem_object_release_memory_region,
17 };
18 
19 /* XXX: Time to vfunc your life up? */
20 void __iomem *
21 i915_gem_object_lmem_io_map_page(struct drm_i915_gem_object *obj,
22 				 unsigned long n)
23 {
24 	resource_size_t offset;
25 
26 	offset = i915_gem_object_get_dma_address(obj, n);
27 	offset -= obj->mm.region->region.start;
28 
29 	return io_mapping_map_wc(&obj->mm.region->iomap, offset, PAGE_SIZE);
30 }
31 
32 void __iomem *
33 i915_gem_object_lmem_io_map_page_atomic(struct drm_i915_gem_object *obj,
34 					unsigned long n)
35 {
36 	resource_size_t offset;
37 
38 	offset = i915_gem_object_get_dma_address(obj, n);
39 	offset -= obj->mm.region->region.start;
40 
41 	return io_mapping_map_atomic_wc(&obj->mm.region->iomap, offset);
42 }
43 
44 void __iomem *
45 i915_gem_object_lmem_io_map(struct drm_i915_gem_object *obj,
46 			    unsigned long n,
47 			    unsigned long size)
48 {
49 	resource_size_t offset;
50 
51 	GEM_BUG_ON(!i915_gem_object_is_contiguous(obj));
52 
53 	offset = i915_gem_object_get_dma_address(obj, n);
54 	offset -= obj->mm.region->region.start;
55 
56 	return io_mapping_map_wc(&obj->mm.region->iomap, offset, size);
57 }
58 
59 bool i915_gem_object_is_lmem(struct drm_i915_gem_object *obj)
60 {
61 	return obj->ops == &i915_gem_lmem_obj_ops;
62 }
63 
64 struct drm_i915_gem_object *
65 i915_gem_object_create_lmem(struct drm_i915_private *i915,
66 			    resource_size_t size,
67 			    unsigned int flags)
68 {
69 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_LMEM],
70 					     size, flags);
71 }
72 
73 struct drm_i915_gem_object *
74 __i915_gem_lmem_object_create(struct intel_memory_region *mem,
75 			      resource_size_t size,
76 			      unsigned int flags)
77 {
78 	static struct lock_class_key lock_class;
79 	struct drm_i915_private *i915 = mem->i915;
80 	struct drm_i915_gem_object *obj;
81 
82 	if (size > BIT(mem->mm.max_order) * mem->mm.chunk_size)
83 		return ERR_PTR(-E2BIG);
84 
85 	obj = i915_gem_object_alloc();
86 	if (!obj)
87 		return ERR_PTR(-ENOMEM);
88 
89 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
90 	i915_gem_object_init(obj, &i915_gem_lmem_obj_ops, &lock_class);
91 
92 	obj->read_domains = I915_GEM_DOMAIN_WC | I915_GEM_DOMAIN_GTT;
93 
94 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_NONE);
95 
96 	i915_gem_object_init_memory_region(obj, mem, flags);
97 
98 	return obj;
99 }
100