1f6ffbd4fSLucas Stach /* SPDX-License-Identifier: GPL-2.0 */
2a8c21a54SThe etnaviv authors /*
3f6ffbd4fSLucas Stach  * Copyright (C) 2015-2018 Etnaviv Project
4a8c21a54SThe etnaviv authors  */
5a8c21a54SThe etnaviv authors 
6a8c21a54SThe etnaviv authors #ifndef __ETNAVIV_DRV_H__
7a8c21a54SThe etnaviv authors #define __ETNAVIV_DRV_H__
8a8c21a54SThe etnaviv authors 
945b64fd9SThomas Zimmermann #include <linux/io.h>
106eae41feSSam Ravnborg #include <linux/list.h>
116eae41feSSam Ravnborg #include <linux/mm_types.h>
126eae41feSSam Ravnborg #include <linux/sizes.h>
13d066b246SRussell King #include <linux/time64.h>
14a8c21a54SThe etnaviv authors #include <linux/types.h>
15764be123SLucas Stach #include <linux/xarray.h>
16a8c21a54SThe etnaviv authors 
1745b64fd9SThomas Zimmermann #include <drm/drm_drv.h>
18a8c21a54SThe etnaviv authors #include <drm/drm_gem.h>
19a8c21a54SThe etnaviv authors #include <drm/etnaviv_drm.h>
20e93b6deeSLucas Stach #include <drm/gpu_scheduler.h>
21a8c21a54SThe etnaviv authors 
22a8c21a54SThe etnaviv authors struct etnaviv_cmdbuf;
23a8c21a54SThe etnaviv authors struct etnaviv_gpu;
24a8c21a54SThe etnaviv authors struct etnaviv_mmu;
25a8c21a54SThe etnaviv authors struct etnaviv_gem_object;
26a8c21a54SThe etnaviv authors struct etnaviv_gem_submit;
2727b67278SLucas Stach struct etnaviv_iommu_global;
28a8c21a54SThe etnaviv authors 
29088880ddSLucas Stach #define ETNAVIV_SOFTPIN_START_ADDRESS	SZ_4M /* must be >= SUBALLOC_SIZE */
30088880ddSLucas Stach 
31a8c21a54SThe etnaviv authors struct etnaviv_file_private {
32*d306788bSLucas Stach 	int id;
3317e4660aSLucas Stach 	struct etnaviv_iommu_context	*mmu;
34e93b6deeSLucas Stach 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
35a8c21a54SThe etnaviv authors };
36a8c21a54SThe etnaviv authors 
37a8c21a54SThe etnaviv authors struct etnaviv_drm_private {
38a8c21a54SThe etnaviv authors 	int num_gpus;
39a8c21a54SThe etnaviv authors 	struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];
40b72af445SLucas Stach 	gfp_t shm_gfp_mask;
41a8c21a54SThe etnaviv authors 
42bffe5db8SLucas Stach 	struct etnaviv_cmdbuf_suballoc *cmdbuf_suballoc;
4327b67278SLucas Stach 	struct etnaviv_iommu_global *mmu_global;
44bffe5db8SLucas Stach 
45*d306788bSLucas Stach 	struct xarray active_contexts;
46*d306788bSLucas Stach 	u32 next_context_id;
47*d306788bSLucas Stach 
48a8c21a54SThe etnaviv authors 	/* list of GEM objects: */
49a8c21a54SThe etnaviv authors 	struct mutex gem_lock;
50a8c21a54SThe etnaviv authors 	struct list_head gem_list;
51a8c21a54SThe etnaviv authors };
52a8c21a54SThe etnaviv authors 
53a8c21a54SThe etnaviv authors int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
54a8c21a54SThe etnaviv authors 		struct drm_file *file);
55a8c21a54SThe etnaviv authors 
56a8c21a54SThe etnaviv authors int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset);
57a8c21a54SThe etnaviv authors struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj);
587938f421SLucas De Marchi int etnaviv_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map);
59a8c21a54SThe etnaviv authors struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
60a8c21a54SThe etnaviv authors 	struct dma_buf_attachment *attach, struct sg_table *sg);
61a8c21a54SThe etnaviv authors int etnaviv_gem_prime_pin(struct drm_gem_object *obj);
62a8c21a54SThe etnaviv authors void etnaviv_gem_prime_unpin(struct drm_gem_object *obj);
63ce3088fdSLucas Stach void *etnaviv_gem_vmap(struct drm_gem_object *obj);
64a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
6538c4a4cfSArnd Bergmann 		struct drm_etnaviv_timespec *timeout);
66a8c21a54SThe etnaviv authors int etnaviv_gem_cpu_fini(struct drm_gem_object *obj);
67a8c21a54SThe etnaviv authors void etnaviv_gem_free_object(struct drm_gem_object *obj);
68a8c21a54SThe etnaviv authors int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
69a8c21a54SThe etnaviv authors 		u32 size, u32 flags, u32 *handle);
70a8c21a54SThe etnaviv authors int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
71a8c21a54SThe etnaviv authors 	uintptr_t ptr, u32 size, u32 flags, u32 *handle);
72a8c21a54SThe etnaviv authors u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
73de08e8efSLucas Stach u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
7427b67278SLucas Stach u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id);
75a8c21a54SThe etnaviv authors void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
76357713ceSChristian Gmeiner void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
77797b0159SLucas Stach void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
7817e4660aSLucas Stach 	struct etnaviv_iommu_context *mmu,
79797b0159SLucas Stach 	unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
80a8c21a54SThe etnaviv authors void etnaviv_validate_init(void);
81a8c21a54SThe etnaviv authors bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,
82a8c21a54SThe etnaviv authors 	u32 *stream, unsigned int size,
83a8c21a54SThe etnaviv authors 	struct drm_etnaviv_gem_submit_reloc *relocs, unsigned int reloc_size);
84a8c21a54SThe etnaviv authors 
85a8c21a54SThe etnaviv authors #ifdef CONFIG_DEBUG_FS
86a8c21a54SThe etnaviv authors void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
87a8c21a54SThe etnaviv authors 	struct seq_file *m);
88a8c21a54SThe etnaviv authors #endif
89a8c21a54SThe etnaviv authors 
90a8c21a54SThe etnaviv authors #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
91a8c21a54SThe etnaviv authors #define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
92a8c21a54SThe etnaviv authors 
93a8c21a54SThe etnaviv authors /*
94a8c21a54SThe etnaviv authors  * Return the storage size of a structure with a variable length array.
95a8c21a54SThe etnaviv authors  * The array is nelem elements of elem_size, where the base structure
96a8c21a54SThe etnaviv authors  * is defined by base.  If the size overflows size_t, return zero.
97a8c21a54SThe etnaviv authors  */
size_vstruct(size_t nelem,size_t elem_size,size_t base)98a8c21a54SThe etnaviv authors static inline size_t size_vstruct(size_t nelem, size_t elem_size, size_t base)
99a8c21a54SThe etnaviv authors {
100a8c21a54SThe etnaviv authors 	if (elem_size && nelem > (SIZE_MAX - base) / elem_size)
101a8c21a54SThe etnaviv authors 		return 0;
102a8c21a54SThe etnaviv authors 	return base + nelem * elem_size;
103a8c21a54SThe etnaviv authors }
104a8c21a54SThe etnaviv authors 
105d066b246SRussell King /*
106d066b246SRussell King  * Etnaviv timeouts are specified wrt CLOCK_MONOTONIC, not jiffies.
107d066b246SRussell King  * We need to calculate the timeout in terms of number of jiffies
108d066b246SRussell King  * between the specified timeout and the current CLOCK_MONOTONIC time.
109d066b246SRussell King  */
etnaviv_timeout_to_jiffies(const struct drm_etnaviv_timespec * timeout)110a8c21a54SThe etnaviv authors static inline unsigned long etnaviv_timeout_to_jiffies(
11138c4a4cfSArnd Bergmann 	const struct drm_etnaviv_timespec *timeout)
112a8c21a54SThe etnaviv authors {
11338c4a4cfSArnd Bergmann 	struct timespec64 ts, to = {
11438c4a4cfSArnd Bergmann 		.tv_sec = timeout->tv_sec,
11538c4a4cfSArnd Bergmann 		.tv_nsec = timeout->tv_nsec,
11638c4a4cfSArnd Bergmann 	};
117a8c21a54SThe etnaviv authors 
118d066b246SRussell King 	ktime_get_ts64(&ts);
119d066b246SRussell King 
120d066b246SRussell King 	/* timeouts before "now" have already expired */
121d066b246SRussell King 	if (timespec64_compare(&to, &ts) <= 0)
122d066b246SRussell King 		return 0;
123d066b246SRussell King 
124d066b246SRussell King 	ts = timespec64_sub(to, ts);
125d066b246SRussell King 
126d066b246SRussell King 	return timespec64_to_jiffies(&ts);
127a8c21a54SThe etnaviv authors }
128a8c21a54SThe etnaviv authors 
129a8c21a54SThe etnaviv authors #endif /* __ETNAVIV_DRV_H__ */
130