2 * Copyright (C) 2015 Etnaviv Project
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef __ETNAVIV_DRV_H__
18 #define __ETNAVIV_DRV_H__
20 #include <linux/kernel.h>
21 #include <linux/clk.h>
22 #include <linux/cpufreq.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/time64.h>
30 #include <linux/types.h>
31 #include <linux/sizes.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/drm_gem.h>
37 #include <drm/etnaviv_drm.h>
38 #include <drm/gpu_scheduler.h>
40 struct etnaviv_cmdbuf;
43 struct etnaviv_gem_object;
44 struct etnaviv_gem_submit;
46 struct etnaviv_file_private {
48 * When per-context address spaces are supported we'd keep track of
49 * the context's page-tables here.
51 struct drm_sched_entity sched_entity[ETNA_MAX_PIPES];
54 struct etnaviv_drm_private {
56 struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];
58 /* list of GEM objects: */
59 struct mutex gem_lock;
60 struct list_head gem_list;
63 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
64 struct drm_file *file);
66 int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma);
67 int etnaviv_gem_fault(struct vm_fault *vmf);
68 int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset);
69 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj);
70 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj);
71 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
72 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
73 struct vm_area_struct *vma);
74 struct reservation_object *etnaviv_gem_prime_res_obj(struct drm_gem_object *obj);
75 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
76 struct dma_buf_attachment *attach, struct sg_table *sg);
77 int etnaviv_gem_prime_pin(struct drm_gem_object *obj);
78 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj);
79 void *etnaviv_gem_vmap(struct drm_gem_object *obj);
80 int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
81 struct timespec *timeout);
82 int etnaviv_gem_cpu_fini(struct drm_gem_object *obj);
83 void etnaviv_gem_free_object(struct drm_gem_object *obj);
84 int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
85 u32 size, u32 flags, u32 *handle);
86 int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
87 uintptr_t ptr, u32 size, u32 flags, u32 *handle);
88 u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
89 u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
90 u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu);
91 void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
92 void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
93 void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
94 unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
95 void etnaviv_validate_init(void);
96 bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,
97 u32 *stream, unsigned int size,
98 struct drm_etnaviv_gem_submit_reloc *relocs, unsigned int reloc_size);
100 #ifdef CONFIG_DEBUG_FS
101 void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
105 #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
106 #define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
109 * Return the storage size of a structure with a variable length array.
110 * The array is nelem elements of elem_size, where the base structure
111 * is defined by base. If the size overflows size_t, return zero.
113 static inline size_t size_vstruct(size_t nelem, size_t elem_size, size_t base)
115 if (elem_size && nelem > (SIZE_MAX - base) / elem_size)
117 return base + nelem * elem_size;
120 /* returns true if fence a comes after fence b */
121 static inline bool fence_after(u32 a, u32 b)
123 return (s32)(a - b) > 0;
126 static inline bool fence_after_eq(u32 a, u32 b)
128 return (s32)(a - b) >= 0;
132 * Etnaviv timeouts are specified wrt CLOCK_MONOTONIC, not jiffies.
133 * We need to calculate the timeout in terms of number of jiffies
134 * between the specified timeout and the current CLOCK_MONOTONIC time.
136 static inline unsigned long etnaviv_timeout_to_jiffies(
137 const struct timespec *timeout)
139 struct timespec64 ts, to;
141 to = timespec_to_timespec64(*timeout);
145 /* timeouts before "now" have already expired */
146 if (timespec64_compare(&to, &ts) <= 0)
149 ts = timespec64_sub(to, ts);
151 return timespec64_to_jiffies(&ts);
154 #endif /* __ETNAVIV_DRV_H__ */