Jordan Crouse | ef3456c | 2013-01-04 16:46:51 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2008-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 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 Dwivedi | 8cb835b | 2012-03-29 17:23:11 -0600 | [diff] [blame] | 24 | #include <linux/mm.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | |
Jordan Crouse | 914de9b | 2012-07-09 13:49:46 -0600 | [diff] [blame] | 26 | #include <mach/kgsl.h> |
| 27 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | #define KGSL_NAME "kgsl" |
| 29 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 30 | /* 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 Boody | 23bfaa2 | 2012-04-24 14:57:45 -0600 | [diff] [blame] | 38 | /* Timestamp window used to detect rollovers (half of integer range) */ |
Jeff Boody | 8614948 | 2012-04-17 09:30:19 -0600 | [diff] [blame] | 39 | #define KGSL_TIMESTAMP_WINDOW 0x80000000 |
| 40 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 41 | /*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 Das | f692074 | 2012-05-01 01:48:37 -0600 | [diff] [blame] | 49 | #define KGSL_PAGETABLE_BASE 0x10000000 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | |
| 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | #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 | |
| 74 | struct kgsl_device; |
Jordan Crouse | ef3456c | 2013-01-04 16:46:51 -0700 | [diff] [blame] | 75 | struct kgsl_context; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 76 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 77 | struct kgsl_driver { |
| 78 | struct cdev cdev; |
| 79 | dev_t major; |
| 80 | struct class *class; |
| 81 | /* Virtual device for managing the core */ |
| 82 | struct device virtdev; |
| 83 | /* Kobjects for storing pagetable and process statistics */ |
| 84 | struct kobject *ptkobj; |
| 85 | struct kobject *prockobj; |
| 86 | struct kgsl_device *devp[KGSL_DEVICE_MAX]; |
| 87 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | /* Global lilst of open processes */ |
| 89 | struct list_head process_list; |
| 90 | /* Global list of pagetables */ |
| 91 | struct list_head pagetable_list; |
| 92 | /* Spinlock for accessing the pagetable list */ |
| 93 | spinlock_t ptlock; |
| 94 | /* Mutex for accessing the process list */ |
| 95 | struct mutex process_mutex; |
| 96 | |
| 97 | /* Mutex for protecting the device list */ |
| 98 | struct mutex devlock; |
| 99 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 100 | void *ptpool; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 101 | |
| 102 | struct { |
| 103 | unsigned int vmalloc; |
| 104 | unsigned int vmalloc_max; |
Harsh Vardhan Dwivedi | f99c263 | 2012-03-15 14:17:11 -0600 | [diff] [blame] | 105 | unsigned int page_alloc; |
| 106 | unsigned int page_alloc_max; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | unsigned int coherent; |
| 108 | unsigned int coherent_max; |
| 109 | unsigned int mapped; |
| 110 | unsigned int mapped_max; |
| 111 | unsigned int histogram[16]; |
| 112 | } stats; |
| 113 | }; |
| 114 | |
| 115 | extern struct kgsl_driver kgsl_driver; |
| 116 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 117 | struct kgsl_pagetable; |
Harsh Vardhan Dwivedi | 8cb835b | 2012-03-29 17:23:11 -0600 | [diff] [blame] | 118 | struct kgsl_memdesc; |
| 119 | |
| 120 | struct kgsl_memdesc_ops { |
| 121 | int (*vmflags)(struct kgsl_memdesc *); |
| 122 | int (*vmfault)(struct kgsl_memdesc *, struct vm_area_struct *, |
| 123 | struct vm_fault *); |
| 124 | void (*free)(struct kgsl_memdesc *memdesc); |
| 125 | int (*map_kernel_mem)(struct kgsl_memdesc *); |
| 126 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 128 | /* Internal definitions for memdesc->priv */ |
Jordan Crouse | 7d3139b | 2012-05-18 10:05:02 -0600 | [diff] [blame] | 129 | #define KGSL_MEMDESC_GUARD_PAGE BIT(0) |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 130 | /* Set if the memdesc is mapped into all pagetables */ |
| 131 | #define KGSL_MEMDESC_GLOBAL BIT(1) |
Jordan Crouse | 7d3139b | 2012-05-18 10:05:02 -0600 | [diff] [blame] | 132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | /* shared memory allocation */ |
| 134 | struct kgsl_memdesc { |
| 135 | struct kgsl_pagetable *pagetable; |
| 136 | void *hostptr; |
| 137 | unsigned int gpuaddr; |
| 138 | unsigned int physaddr; |
| 139 | unsigned int size; |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 140 | unsigned int priv; /* Internal flags and settings */ |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 141 | struct scatterlist *sg; |
Rajeev Kulkarni | 8dfdc336 | 2012-11-22 00:22:32 -0800 | [diff] [blame] | 142 | unsigned int sglen; /* Active entries in the sglist */ |
| 143 | unsigned int sglen_alloc; /* Allocated entries in the sglist */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | struct kgsl_memdesc_ops *ops; |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 145 | unsigned int flags; /* Flags set from userspace */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 148 | /* List of different memory entry types */ |
| 149 | |
| 150 | #define KGSL_MEM_ENTRY_KERNEL 0 |
| 151 | #define KGSL_MEM_ENTRY_PMEM 1 |
| 152 | #define KGSL_MEM_ENTRY_ASHMEM 2 |
| 153 | #define KGSL_MEM_ENTRY_USER 3 |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 154 | #define KGSL_MEM_ENTRY_ION 4 |
| 155 | #define KGSL_MEM_ENTRY_MAX 5 |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 156 | |
Jordan Crouse | 9610b6b | 2012-03-16 14:53:42 -0600 | [diff] [blame] | 157 | /* List of flags */ |
| 158 | |
| 159 | #define KGSL_MEM_ENTRY_FROZEN (1 << 0) |
| 160 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | struct kgsl_mem_entry { |
| 162 | struct kref refcount; |
| 163 | struct kgsl_memdesc memdesc; |
| 164 | int memtype; |
Jordan Crouse | 9610b6b | 2012-03-16 14:53:42 -0600 | [diff] [blame] | 165 | int flags; |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 166 | void *priv_data; |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 167 | struct rb_node node; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 168 | unsigned int context_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | /* back pointer to private structure under whose context this |
| 170 | * allocation is made */ |
| 171 | struct kgsl_process_private *priv; |
| 172 | }; |
| 173 | |
| 174 | #ifdef CONFIG_MSM_KGSL_MMU_PAGE_FAULT |
| 175 | #define MMU_CONFIG 2 |
| 176 | #else |
| 177 | #define MMU_CONFIG 1 |
| 178 | #endif |
| 179 | |
| 180 | void kgsl_mem_entry_destroy(struct kref *kref); |
Harsh Vardhan Dwivedi | 715fb83 | 2012-05-18 00:24:18 -0600 | [diff] [blame] | 181 | int kgsl_postmortem_dump(struct kgsl_device *device, int manual); |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 182 | |
Shubhraprakash Das | 3cf33be | 2012-08-16 22:42:55 -0700 | [diff] [blame] | 183 | struct kgsl_mem_entry *kgsl_get_mem_entry(struct kgsl_device *device, |
| 184 | unsigned int ptbase, unsigned int gpuaddr, unsigned int size); |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 185 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | struct kgsl_mem_entry *kgsl_sharedmem_find_region( |
| 187 | struct kgsl_process_private *private, unsigned int gpuaddr, |
| 188 | size_t size); |
| 189 | |
Jeremy Gebben | 158a5c0 | 2012-09-24 14:27:25 -0600 | [diff] [blame] | 190 | void kgsl_get_memory_usage(char *str, size_t len, unsigned int memflags); |
| 191 | |
Shubhraprakash Das | cb06807 | 2012-06-07 17:52:41 -0600 | [diff] [blame] | 192 | int kgsl_add_event(struct kgsl_device *device, u32 id, u32 ts, |
| 193 | void (*cb)(struct kgsl_device *, void *, u32, u32), void *priv, |
| 194 | void *owner); |
| 195 | |
| 196 | void kgsl_cancel_events(struct kgsl_device *device, |
| 197 | void *owner); |
| 198 | |
Jordan Crouse | ef3456c | 2013-01-04 16:46:51 -0700 | [diff] [blame] | 199 | void kgsl_cancel_events_ctxt(struct kgsl_device *device, |
| 200 | struct kgsl_context *context); |
| 201 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 202 | extern const struct dev_pm_ops kgsl_pm_ops; |
| 203 | |
| 204 | struct early_suspend; |
| 205 | int kgsl_suspend_driver(struct platform_device *pdev, pm_message_t state); |
| 206 | int kgsl_resume_driver(struct platform_device *pdev); |
| 207 | void kgsl_early_suspend_driver(struct early_suspend *h); |
| 208 | void kgsl_late_resume_driver(struct early_suspend *h); |
| 209 | |
| 210 | #ifdef CONFIG_MSM_KGSL_DRM |
| 211 | extern int kgsl_drm_init(struct platform_device *dev); |
| 212 | extern void kgsl_drm_exit(void); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | #else |
| 214 | static inline int kgsl_drm_init(struct platform_device *dev) |
| 215 | { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static inline void kgsl_drm_exit(void) |
| 220 | { |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | static inline int kgsl_gpuaddr_in_memdesc(const struct kgsl_memdesc *memdesc, |
Jeremy Gebben | 16e80fa | 2011-11-30 15:56:29 -0700 | [diff] [blame] | 225 | unsigned int gpuaddr, unsigned int size) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | { |
Jeremy Gebben | 16e80fa | 2011-11-30 15:56:29 -0700 | [diff] [blame] | 227 | if (gpuaddr >= memdesc->gpuaddr && |
| 228 | ((gpuaddr + size) <= (memdesc->gpuaddr + memdesc->size))) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | return 0; |
| 232 | } |
Jordan Crouse | 17d6d8b | 2012-04-18 09:31:09 -0600 | [diff] [blame] | 233 | |
| 234 | static inline void *kgsl_memdesc_map(struct kgsl_memdesc *memdesc) |
| 235 | { |
Shubhraprakash Das | 80af30d | 2012-05-24 18:22:54 -0600 | [diff] [blame] | 236 | if (memdesc->hostptr == NULL && memdesc->ops && |
| 237 | memdesc->ops->map_kernel_mem) |
Jordan Crouse | 17d6d8b | 2012-04-18 09:31:09 -0600 | [diff] [blame] | 238 | memdesc->ops->map_kernel_mem(memdesc); |
| 239 | |
| 240 | return memdesc->hostptr; |
| 241 | } |
| 242 | |
Harsh Vardhan Dwivedi | 8cb835b | 2012-03-29 17:23:11 -0600 | [diff] [blame] | 243 | static inline uint8_t *kgsl_gpuaddr_to_vaddr(struct kgsl_memdesc *memdesc, |
Jeremy Gebben | 16e80fa | 2011-11-30 15:56:29 -0700 | [diff] [blame] | 244 | unsigned int gpuaddr) |
| 245 | { |
Jordan Crouse | 17d6d8b | 2012-04-18 09:31:09 -0600 | [diff] [blame] | 246 | void *hostptr = NULL; |
Jeremy Gebben | 16e80fa | 2011-11-30 15:56:29 -0700 | [diff] [blame] | 247 | |
Jordan Crouse | 17d6d8b | 2012-04-18 09:31:09 -0600 | [diff] [blame] | 248 | if ((gpuaddr >= memdesc->gpuaddr) && |
| 249 | (gpuaddr < (memdesc->gpuaddr + memdesc->size))) |
| 250 | hostptr = kgsl_memdesc_map(memdesc); |
| 251 | |
| 252 | return hostptr != NULL ? hostptr + (gpuaddr - memdesc->gpuaddr) : NULL; |
Jeremy Gebben | 16e80fa | 2011-11-30 15:56:29 -0700 | [diff] [blame] | 253 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 254 | |
Jeff Boody | 23bfaa2 | 2012-04-24 14:57:45 -0600 | [diff] [blame] | 255 | static inline int timestamp_cmp(unsigned int a, unsigned int b) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | { |
Jeff Boody | 23bfaa2 | 2012-04-24 14:57:45 -0600 | [diff] [blame] | 257 | /* check for equal */ |
| 258 | if (a == b) |
Jordan Crouse | e6239dd | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | |
Jeff Boody | 23bfaa2 | 2012-04-24 14:57:45 -0600 | [diff] [blame] | 261 | /* check for greater-than for non-rollover case */ |
| 262 | if ((a > b) && (a - b < KGSL_TIMESTAMP_WINDOW)) |
| 263 | return 1; |
| 264 | |
| 265 | /* check for greater-than for rollover case |
| 266 | * note that <= is required to ensure that consistent |
| 267 | * results are returned for values whose difference is |
| 268 | * equal to the window size |
| 269 | */ |
| 270 | a += KGSL_TIMESTAMP_WINDOW; |
| 271 | b += KGSL_TIMESTAMP_WINDOW; |
| 272 | return ((a > b) && (a - b <= KGSL_TIMESTAMP_WINDOW)) ? 1 : -1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static inline void |
| 276 | kgsl_mem_entry_get(struct kgsl_mem_entry *entry) |
| 277 | { |
| 278 | kref_get(&entry->refcount); |
| 279 | } |
| 280 | |
| 281 | static inline void |
| 282 | kgsl_mem_entry_put(struct kgsl_mem_entry *entry) |
| 283 | { |
| 284 | kref_put(&entry->refcount, kgsl_mem_entry_destroy); |
| 285 | } |
| 286 | |
| 287 | #endif /* __KGSL_H */ |