blob: 72a251adcc9a6692db1741a88730bfa893786367 [file] [log] [blame]
Jordan Crouse0fdf3a02012-03-16 14:53:41 -06001/* Copyright (c) 2008-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#ifndef __KGSL_H
14#define __KGSL_H
15
16#include <linux/types.h>
17#include <linux/msm_kgsl.h>
18#include <linux/platform_device.h>
19#include <linux/clk.h>
20#include <linux/interrupt.h>
21#include <linux/mutex.h>
22#include <linux/cdev.h>
23#include <linux/regulator/consumer.h>
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -060024#include <linux/mm.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
Jordan Crouse914de9b2012-07-09 13:49:46 -060026#include <mach/kgsl.h>
27
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#define KGSL_NAME "kgsl"
29
Carter Cooper7e7f02e2012-02-15 09:36:31 -070030/* The number of memstore arrays limits the number of contexts allowed.
31 * If more contexts are needed, update multiple for MEMSTORE_SIZE
32 */
33#define KGSL_MEMSTORE_SIZE ((int)(PAGE_SIZE * 2))
34#define KGSL_MEMSTORE_GLOBAL (0)
35#define KGSL_MEMSTORE_MAX (KGSL_MEMSTORE_SIZE / \
36 sizeof(struct kgsl_devmemstore) - 1)
37
Jeff Boody23bfaa22012-04-24 14:57:45 -060038/* Timestamp window used to detect rollovers (half of integer range) */
Jeff Boody86149482012-04-17 09:30:19 -060039#define KGSL_TIMESTAMP_WINDOW 0x80000000
40
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041/*cache coherency ops */
42#define DRM_KGSL_GEM_CACHE_OP_TO_DEV 0x0001
43#define DRM_KGSL_GEM_CACHE_OP_FROM_DEV 0x0002
44
45/* The size of each entry in a page table */
46#define KGSL_PAGETABLE_ENTRY_SIZE 4
47
48/* Pagetable Virtual Address base */
Shubhraprakash Dasf6920742012-05-01 01:48:37 -060049#define KGSL_PAGETABLE_BASE 0x10000000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51/* Extra accounting entries needed in the pagetable */
52#define KGSL_PT_EXTRA_ENTRIES 16
53
54#define KGSL_PAGETABLE_ENTRIES(_sz) (((_sz) >> PAGE_SHIFT) + \
55 KGSL_PT_EXTRA_ENTRIES)
56
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057#ifdef CONFIG_KGSL_PER_PROCESS_PAGE_TABLE
58#define KGSL_PAGETABLE_COUNT (CONFIG_MSM_KGSL_PAGE_TABLE_COUNT)
59#else
60#define KGSL_PAGETABLE_COUNT 1
61#endif
62
63/* Casting using container_of() for structures that kgsl owns. */
64#define KGSL_CONTAINER_OF(ptr, type, member) \
65 container_of(ptr, type, member)
66
67/* A macro for memory statistics - add the new size to the stat and if
68 the statisic is greater then _max, set _max
69*/
70
71#define KGSL_STATS_ADD(_size, _stat, _max) \
72 do { _stat += (_size); if (_stat > _max) _max = _stat; } while (0)
73
74struct kgsl_device;
75
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076struct kgsl_driver {
77 struct cdev cdev;
78 dev_t major;
79 struct class *class;
80 /* Virtual device for managing the core */
81 struct device virtdev;
82 /* Kobjects for storing pagetable and process statistics */
83 struct kobject *ptkobj;
84 struct kobject *prockobj;
85 struct kgsl_device *devp[KGSL_DEVICE_MAX];
86
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087 /* Global lilst of open processes */
88 struct list_head process_list;
89 /* Global list of pagetables */
90 struct list_head pagetable_list;
91 /* Spinlock for accessing the pagetable list */
92 spinlock_t ptlock;
93 /* Mutex for accessing the process list */
94 struct mutex process_mutex;
95
96 /* Mutex for protecting the device list */
97 struct mutex devlock;
98
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060099 void *ptpool;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100
101 struct {
102 unsigned int vmalloc;
103 unsigned int vmalloc_max;
Harsh Vardhan Dwivedif99c2632012-03-15 14:17:11 -0600104 unsigned int page_alloc;
105 unsigned int page_alloc_max;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106 unsigned int coherent;
107 unsigned int coherent_max;
108 unsigned int mapped;
109 unsigned int mapped_max;
110 unsigned int histogram[16];
111 } stats;
112};
113
114extern struct kgsl_driver kgsl_driver;
115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116struct kgsl_pagetable;
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600117struct kgsl_memdesc;
118
119struct kgsl_memdesc_ops {
120 int (*vmflags)(struct kgsl_memdesc *);
121 int (*vmfault)(struct kgsl_memdesc *, struct vm_area_struct *,
122 struct vm_fault *);
123 void (*free)(struct kgsl_memdesc *memdesc);
124 int (*map_kernel_mem)(struct kgsl_memdesc *);
125};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126
Jordan Crousedc67dfb2012-10-25 09:41:46 -0600127/* Internal definitions for memdesc->priv */
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600128#define KGSL_MEMDESC_GUARD_PAGE BIT(0)
Jordan Crousedc67dfb2012-10-25 09:41:46 -0600129/* Set if the memdesc is mapped into all pagetables */
130#define KGSL_MEMDESC_GLOBAL BIT(1)
Jordan Crouse7d3139b2012-05-18 10:05:02 -0600131
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132/* shared memory allocation */
133struct kgsl_memdesc {
134 struct kgsl_pagetable *pagetable;
135 void *hostptr;
136 unsigned int gpuaddr;
137 unsigned int physaddr;
138 unsigned int size;
Jordan Crousedc67dfb2012-10-25 09:41:46 -0600139 unsigned int priv; /* Internal flags and settings */
Jordan Croused17e9aa2011-10-12 16:57:48 -0600140 struct scatterlist *sg;
Rajeev Kulkarni8dfdc3362012-11-22 00:22:32 -0800141 unsigned int sglen; /* Active entries in the sglist */
142 unsigned int sglen_alloc; /* Allocated entries in the sglist */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 struct kgsl_memdesc_ops *ops;
Jordan Crousedc67dfb2012-10-25 09:41:46 -0600144 unsigned int flags; /* Flags set from userspace */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145};
146
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600147/* List of different memory entry types */
148
149#define KGSL_MEM_ENTRY_KERNEL 0
150#define KGSL_MEM_ENTRY_PMEM 1
151#define KGSL_MEM_ENTRY_ASHMEM 2
152#define KGSL_MEM_ENTRY_USER 3
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600153#define KGSL_MEM_ENTRY_ION 4
154#define KGSL_MEM_ENTRY_MAX 5
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600155
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600156/* List of flags */
157
158#define KGSL_MEM_ENTRY_FROZEN (1 << 0)
159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160struct kgsl_mem_entry {
161 struct kref refcount;
162 struct kgsl_memdesc memdesc;
163 int memtype;
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600164 int flags;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600165 void *priv_data;
Jordan Crousec9559e42012-04-05 16:55:56 -0600166 struct rb_node node;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700167 unsigned int context_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 /* back pointer to private structure under whose context this
169 * allocation is made */
170 struct kgsl_process_private *priv;
171};
172
173#ifdef CONFIG_MSM_KGSL_MMU_PAGE_FAULT
174#define MMU_CONFIG 2
175#else
176#define MMU_CONFIG 1
177#endif
178
179void kgsl_mem_entry_destroy(struct kref *kref);
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -0600180int kgsl_postmortem_dump(struct kgsl_device *device, int manual);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -0600181
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700182struct kgsl_mem_entry *kgsl_get_mem_entry(struct kgsl_device *device,
183 unsigned int ptbase, unsigned int gpuaddr, unsigned int size);
Jordan Crouse0fdf3a02012-03-16 14:53:41 -0600184
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185struct kgsl_mem_entry *kgsl_sharedmem_find_region(
186 struct kgsl_process_private *private, unsigned int gpuaddr,
187 size_t size);
188
Jeremy Gebben158a5c02012-09-24 14:27:25 -0600189void kgsl_get_memory_usage(char *str, size_t len, unsigned int memflags);
190
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600191int kgsl_add_event(struct kgsl_device *device, u32 id, u32 ts,
192 void (*cb)(struct kgsl_device *, void *, u32, u32), void *priv,
193 void *owner);
194
195void kgsl_cancel_events(struct kgsl_device *device,
196 void *owner);
197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198extern const struct dev_pm_ops kgsl_pm_ops;
199
200struct early_suspend;
201int kgsl_suspend_driver(struct platform_device *pdev, pm_message_t state);
202int kgsl_resume_driver(struct platform_device *pdev);
203void kgsl_early_suspend_driver(struct early_suspend *h);
204void kgsl_late_resume_driver(struct early_suspend *h);
205
206#ifdef CONFIG_MSM_KGSL_DRM
207extern int kgsl_drm_init(struct platform_device *dev);
208extern void kgsl_drm_exit(void);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209#else
210static inline int kgsl_drm_init(struct platform_device *dev)
211{
212 return 0;
213}
214
215static inline void kgsl_drm_exit(void)
216{
217}
218#endif
219
220static inline int kgsl_gpuaddr_in_memdesc(const struct kgsl_memdesc *memdesc,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700221 unsigned int gpuaddr, unsigned int size)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222{
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700223 if (gpuaddr >= memdesc->gpuaddr &&
224 ((gpuaddr + size) <= (memdesc->gpuaddr + memdesc->size))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 return 1;
226 }
227 return 0;
228}
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600229
230static inline void *kgsl_memdesc_map(struct kgsl_memdesc *memdesc)
231{
Shubhraprakash Das80af30d2012-05-24 18:22:54 -0600232 if (memdesc->hostptr == NULL && memdesc->ops &&
233 memdesc->ops->map_kernel_mem)
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600234 memdesc->ops->map_kernel_mem(memdesc);
235
236 return memdesc->hostptr;
237}
238
Harsh Vardhan Dwivedi8cb835b2012-03-29 17:23:11 -0600239static inline uint8_t *kgsl_gpuaddr_to_vaddr(struct kgsl_memdesc *memdesc,
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700240 unsigned int gpuaddr)
241{
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600242 void *hostptr = NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700243
Jordan Crouse17d6d8b2012-04-18 09:31:09 -0600244 if ((gpuaddr >= memdesc->gpuaddr) &&
245 (gpuaddr < (memdesc->gpuaddr + memdesc->size)))
246 hostptr = kgsl_memdesc_map(memdesc);
247
248 return hostptr != NULL ? hostptr + (gpuaddr - memdesc->gpuaddr) : NULL;
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700249}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250
Jeff Boody23bfaa22012-04-24 14:57:45 -0600251static inline int timestamp_cmp(unsigned int a, unsigned int b)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252{
Jeff Boody23bfaa22012-04-24 14:57:45 -0600253 /* check for equal */
254 if (a == b)
Jordan Crousee6239dd2011-11-17 13:39:21 -0700255 return 0;
256
Jeff Boody23bfaa22012-04-24 14:57:45 -0600257 /* check for greater-than for non-rollover case */
258 if ((a > b) && (a - b < KGSL_TIMESTAMP_WINDOW))
259 return 1;
260
261 /* check for greater-than for rollover case
262 * note that <= is required to ensure that consistent
263 * results are returned for values whose difference is
264 * equal to the window size
265 */
266 a += KGSL_TIMESTAMP_WINDOW;
267 b += KGSL_TIMESTAMP_WINDOW;
268 return ((a > b) && (a - b <= KGSL_TIMESTAMP_WINDOW)) ? 1 : -1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269}
270
271static inline void
272kgsl_mem_entry_get(struct kgsl_mem_entry *entry)
273{
274 kref_get(&entry->refcount);
275}
276
277static inline void
278kgsl_mem_entry_put(struct kgsl_mem_entry *entry)
279{
280 kref_put(&entry->refcount, kgsl_mem_entry_destroy);
281}
282
283#endif /* __KGSL_H */