blob: ae164bc7aa27174230f1162d9a989bc6ce17aedd [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2002,2007-2017, The Linux Foundation. All rights reserved.
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_DEVICE_H
14#define __KGSL_DEVICE_H
15
16#include <linux/slab.h>
17#include <linux/idr.h>
18#include <linux/pm_qos.h>
19#include <linux/sched.h>
20
21#include "kgsl.h"
22#include "kgsl_mmu.h"
23#include "kgsl_pwrctrl.h"
24#include "kgsl_log.h"
25#include "kgsl_pwrscale.h"
26#include "kgsl_snapshot.h"
27#include "kgsl_sharedmem.h"
28#include "kgsl_drawobj.h"
29
30#define KGSL_IOCTL_FUNC(_cmd, _func) \
31 [_IOC_NR((_cmd))] = \
32 { .cmd = (_cmd), .func = (_func) }
33
34/*
35 * KGSL device state is initialized to INIT when platform_probe *
36 * successfully initialized the device. Once a device has been opened *
37 * (started) it becomes active. NAP implies that only low latency *
38 * resources (for now clocks on some platforms) are off. SLEEP implies *
39 * that the KGSL module believes a device is idle (has been inactive *
40 * past its timer) and all system resources are released. SUSPEND is *
41 * requested by the kernel and will be enforced upon all open devices. *
42 */
43
44#define KGSL_STATE_NONE 0x00000000
45#define KGSL_STATE_INIT 0x00000001
46#define KGSL_STATE_ACTIVE 0x00000002
47#define KGSL_STATE_NAP 0x00000004
48#define KGSL_STATE_SUSPEND 0x00000010
49#define KGSL_STATE_AWARE 0x00000020
50#define KGSL_STATE_SLUMBER 0x00000080
51
52/**
53 * enum kgsl_event_results - result codes passed to an event callback when the
54 * event is retired or cancelled
55 * @KGSL_EVENT_RETIRED: The timestamp associated with the event retired
56 * successflly
57 * @KGSL_EVENT_CANCELLED: The event was cancelled before the event was fired
58 */
59enum kgsl_event_results {
60 KGSL_EVENT_RETIRED = 1,
61 KGSL_EVENT_CANCELLED = 2,
62};
63
64#define KGSL_FLAG_WAKE_ON_TOUCH BIT(0)
65
66/*
67 * "list" of event types for ftrace symbolic magic
68 */
69
70#define KGSL_EVENT_TYPES \
71 { KGSL_EVENT_RETIRED, "retired" }, \
72 { KGSL_EVENT_CANCELLED, "cancelled" }
73
74#define KGSL_CONTEXT_FLAGS \
75 { KGSL_CONTEXT_NO_GMEM_ALLOC, "NO_GMEM_ALLOC" }, \
76 { KGSL_CONTEXT_PREAMBLE, "PREAMBLE" }, \
77 { KGSL_CONTEXT_TRASH_STATE, "TRASH_STATE" }, \
78 { KGSL_CONTEXT_CTX_SWITCH, "CTX_SWITCH" }, \
79 { KGSL_CONTEXT_PER_CONTEXT_TS, "PER_CONTEXT_TS" }, \
80 { KGSL_CONTEXT_USER_GENERATED_TS, "USER_TS" }, \
81 { KGSL_CONTEXT_NO_FAULT_TOLERANCE, "NO_FT" }, \
82 { KGSL_CONTEXT_PWR_CONSTRAINT, "PWR" }, \
83 { KGSL_CONTEXT_SAVE_GMEM, "SAVE_GMEM" }
84
85#define KGSL_CONTEXT_TYPES \
86 { KGSL_CONTEXT_TYPE_ANY, "ANY" }, \
87 { KGSL_CONTEXT_TYPE_GL, "GL" }, \
88 { KGSL_CONTEXT_TYPE_CL, "CL" }, \
89 { KGSL_CONTEXT_TYPE_C2D, "C2D" }, \
90 { KGSL_CONTEXT_TYPE_RS, "RS" }
91
92#define KGSL_CONTEXT_ID(_context) \
93 ((_context != NULL) ? (_context)->id : KGSL_MEMSTORE_GLOBAL)
94
95/* Allocate 600K for the snapshot static region*/
96#define KGSL_SNAPSHOT_MEMSIZE (600 * 1024)
97
98struct kgsl_device;
99struct platform_device;
100struct kgsl_device_private;
101struct kgsl_context;
102struct kgsl_power_stats;
103struct kgsl_event;
104struct kgsl_snapshot;
105
106struct kgsl_functable {
107 /* Mandatory functions - these functions must be implemented
108 * by the client device. The driver will not check for a NULL
109 * pointer before calling the hook.
110 */
111 void (*regread)(struct kgsl_device *device,
112 unsigned int offsetwords, unsigned int *value);
113 void (*regwrite)(struct kgsl_device *device,
114 unsigned int offsetwords, unsigned int value);
115 int (*idle)(struct kgsl_device *device);
116 bool (*isidle)(struct kgsl_device *device);
117 int (*suspend_context)(struct kgsl_device *device);
118 int (*init)(struct kgsl_device *device);
119 int (*start)(struct kgsl_device *device, int priority);
120 int (*stop)(struct kgsl_device *device);
121 int (*getproperty)(struct kgsl_device *device,
122 unsigned int type, void __user *value,
123 size_t sizebytes);
124 int (*getproperty_compat)(struct kgsl_device *device,
125 unsigned int type, void __user *value,
126 size_t sizebytes);
127 int (*waittimestamp)(struct kgsl_device *device,
128 struct kgsl_context *context, unsigned int timestamp,
129 unsigned int msecs);
130 int (*readtimestamp)(struct kgsl_device *device, void *priv,
131 enum kgsl_timestamp_type type, unsigned int *timestamp);
132 int (*queue_cmds)(struct kgsl_device_private *dev_priv,
133 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
134 uint32_t count, uint32_t *timestamp);
135 void (*power_stats)(struct kgsl_device *device,
136 struct kgsl_power_stats *stats);
137 unsigned int (*gpuid)(struct kgsl_device *device, unsigned int *chipid);
138 void (*snapshot)(struct kgsl_device *device,
139 struct kgsl_snapshot *snapshot, struct kgsl_context *context);
140 irqreturn_t (*irq_handler)(struct kgsl_device *device);
141 int (*drain)(struct kgsl_device *device);
142 /*
143 * Optional functions - these functions are not mandatory. The
144 * driver will check that the function pointer is not NULL before
145 * calling the hook
146 */
147 struct kgsl_context *(*drawctxt_create)(struct kgsl_device_private *,
148 uint32_t *flags);
149 void (*drawctxt_detach)(struct kgsl_context *context);
150 void (*drawctxt_destroy)(struct kgsl_context *context);
151 void (*drawctxt_dump)(struct kgsl_device *device,
152 struct kgsl_context *context);
153 long (*ioctl)(struct kgsl_device_private *dev_priv,
154 unsigned int cmd, unsigned long arg);
155 long (*compat_ioctl)(struct kgsl_device_private *dev_priv,
156 unsigned int cmd, unsigned long arg);
157 int (*setproperty)(struct kgsl_device_private *dev_priv,
158 unsigned int type, void __user *value,
159 unsigned int sizebytes);
160 int (*setproperty_compat)(struct kgsl_device_private *dev_priv,
161 unsigned int type, void __user *value,
162 unsigned int sizebytes);
163 void (*drawctxt_sched)(struct kgsl_device *device,
164 struct kgsl_context *context);
165 void (*resume)(struct kgsl_device *device);
166 int (*regulator_enable)(struct kgsl_device *);
167 bool (*is_hw_collapsible)(struct kgsl_device *);
168 void (*regulator_disable)(struct kgsl_device *);
169 void (*pwrlevel_change_settings)(struct kgsl_device *device,
170 unsigned int prelevel, unsigned int postlevel, bool post);
171 void (*regulator_disable_poll)(struct kgsl_device *device);
172 void (*clk_set_options)(struct kgsl_device *device,
173 const char *name, struct clk *clk);
174 void (*gpu_model)(struct kgsl_device *device, char *str,
175 size_t bufsz);
176};
177
178struct kgsl_ioctl {
179 unsigned int cmd;
180 long (*func)(struct kgsl_device_private *, unsigned int, void *);
181};
182
183long kgsl_ioctl_helper(struct file *filep, unsigned int cmd, unsigned long arg,
184 const struct kgsl_ioctl *cmds, int len);
185
186/* Flag to mark the memobj_node as a preamble */
187#define MEMOBJ_PREAMBLE BIT(0)
188/* Flag to mark that the memobj_node should not go to the hadrware */
189#define MEMOBJ_SKIP BIT(1)
190
191/**
192 * struct kgsl_memobj_node - Memory object descriptor
193 * @node: Local list node for the object
194 * @id: GPU memory ID for the object
195 * offset: Offset within the object
196 * @gpuaddr: GPU address for the object
197 * @flags: External flags passed by the user
198 * @priv: Internal flags set by the driver
199 */
200struct kgsl_memobj_node {
201 struct list_head node;
202 unsigned int id;
203 uint64_t offset;
204 uint64_t gpuaddr;
205 uint64_t size;
206 unsigned long flags;
207 unsigned long priv;
208};
209
Tarun Karra2b8b3632016-11-14 16:38:27 -0800210/**
211 * struct kgsl_sparseobj_node - Sparse object descriptor
212 * @node: Local list node for the sparse cmdbatch
213 * @virt_id: Virtual ID to bind/unbind
214 * @obj: struct kgsl_sparse_binding_object
215 */
216struct kgsl_sparseobj_node {
217 struct list_head node;
218 unsigned int virt_id;
219 struct kgsl_sparse_binding_object obj;
220};
221
Shrenuj Bansala419c792016-10-20 14:05:11 -0700222struct kgsl_device {
223 struct device *dev;
224 const char *name;
225 unsigned int ver_major;
226 unsigned int ver_minor;
227 uint32_t flags;
228 enum kgsl_deviceid id;
229
230 /* Starting physical address for GPU registers */
231 unsigned long reg_phys;
232
233 /* Starting Kernel virtual address for GPU registers */
234 void __iomem *reg_virt;
235
236 /* Total memory size for all GPU registers */
237 unsigned int reg_len;
238
239 /* Kernel virtual address for GPU shader memory */
240 void __iomem *shader_mem_virt;
241
242 /* Starting physical address for GPU shader memory */
243 unsigned long shader_mem_phys;
244
245 /* GPU shader memory size */
246 unsigned int shader_mem_len;
247 struct kgsl_memdesc memstore;
248 struct kgsl_memdesc scratch;
249 const char *iomemname;
250 const char *shadermemname;
251
252 struct kgsl_mmu mmu;
253 struct completion hwaccess_gate;
254 struct completion halt_gate;
255 const struct kgsl_functable *ftbl;
256 struct work_struct idle_check_ws;
257 struct timer_list idle_timer;
258 struct kgsl_pwrctrl pwrctrl;
259 int open_count;
260
261 struct mutex mutex;
262 uint32_t state;
263 uint32_t requested_state;
264
265 atomic_t active_cnt;
266
267 wait_queue_head_t wait_queue;
268 wait_queue_head_t active_cnt_wq;
269 struct platform_device *pdev;
270 struct dentry *d_debugfs;
271 struct idr context_idr;
272 rwlock_t context_lock;
273
274 struct {
275 void *ptr;
276 size_t size;
277 } snapshot_memory;
278
279 struct kgsl_snapshot *snapshot;
280
281 u32 snapshot_faultcount; /* Total number of faults since boot */
282 bool force_panic; /* Force panic after snapshot dump */
283
284 /* Use CP Crash dumper to get GPU snapshot*/
285 bool snapshot_crashdumper;
286
287 struct kobject snapshot_kobj;
288
289 struct kobject ppd_kobj;
290
291 /* Logging levels */
292 int cmd_log;
293 int ctxt_log;
294 int drv_log;
295 int mem_log;
296 int pwr_log;
297 struct kgsl_pwrscale pwrscale;
298
299 int reset_counter; /* Track how many GPU core resets have occurred */
300 struct workqueue_struct *events_wq;
301
302 struct device *busmondev; /* pseudo dev for GPU BW voting governor */
303
304 /* Number of active contexts seen globally for this device */
305 int active_context_count;
306 struct kobject *gpu_sysfs_kobj;
307};
308
309#define KGSL_MMU_DEVICE(_mmu) \
310 container_of((_mmu), struct kgsl_device, mmu)
311
312#define KGSL_DEVICE_COMMON_INIT(_dev) \
313 .hwaccess_gate = COMPLETION_INITIALIZER((_dev).hwaccess_gate),\
314 .halt_gate = COMPLETION_INITIALIZER((_dev).halt_gate),\
315 .idle_check_ws = __WORK_INITIALIZER((_dev).idle_check_ws,\
316 kgsl_idle_check),\
317 .context_idr = IDR_INIT((_dev).context_idr),\
318 .wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).wait_queue),\
319 .active_cnt_wq = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).active_cnt_wq),\
320 .mutex = __MUTEX_INITIALIZER((_dev).mutex),\
321 .state = KGSL_STATE_NONE,\
322 .ver_major = DRIVER_VERSION_MAJOR,\
323 .ver_minor = DRIVER_VERSION_MINOR
324
325
326/**
327 * enum bits for struct kgsl_context.priv
328 * @KGSL_CONTEXT_PRIV_DETACHED - The context has been destroyed by userspace
329 * and is no longer using the gpu.
330 * @KGSL_CONTEXT_PRIV_INVALID - The context has been destroyed by the kernel
331 * because it caused a GPU fault.
332 * @KGSL_CONTEXT_PRIV_PAGEFAULT - The context has caused a page fault.
333 * @KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC - this value and higher values are
334 * reserved for devices specific use.
335 */
336enum kgsl_context_priv {
337 KGSL_CONTEXT_PRIV_DETACHED = 0,
338 KGSL_CONTEXT_PRIV_INVALID,
339 KGSL_CONTEXT_PRIV_PAGEFAULT,
340 KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC = 16,
341};
342
343struct kgsl_process_private;
344
345/**
346 * struct kgsl_context - The context fields that are valid for a user defined
347 * context
348 * @refcount: kref object for reference counting the context
349 * @id: integer identifier for the context
350 * @priority; The context's priority to submit commands to GPU
351 * @tid: task that created this context.
352 * @dev_priv: pointer to the owning device instance
353 * @proc_priv: pointer to process private, the process that allocated the
354 * context
355 * @priv: in-kernel context flags, use KGSL_CONTEXT_* values
356 * @reset_status: status indication whether a gpu reset occurred and whether
357 * this context was responsible for causing it
358 * @timeline: sync timeline used to create fences that can be signaled when a
359 * sync_pt timestamp expires
360 * @events: A kgsl_event_group for this context - contains the list of GPU
361 * events
362 * @flags: flags from userspace controlling the behavior of this context
363 * @pwr_constraint: power constraint from userspace for this context
364 * @fault_count: number of times gpu hanged in last _context_throttle_time ms
365 * @fault_time: time of the first gpu hang in last _context_throttle_time ms
366 */
367struct kgsl_context {
368 struct kref refcount;
369 uint32_t id;
370 uint32_t priority;
371 pid_t tid;
372 struct kgsl_device_private *dev_priv;
373 struct kgsl_process_private *proc_priv;
374 unsigned long priv;
375 struct kgsl_device *device;
376 unsigned int reset_status;
377 struct sync_timeline *timeline;
378 struct kgsl_event_group events;
379 unsigned int flags;
380 struct kgsl_pwr_constraint pwr_constraint;
381 unsigned int fault_count;
382 unsigned long fault_time;
383};
384
385#define _context_comm(_c) \
386 (((_c) && (_c)->proc_priv) ? (_c)->proc_priv->comm : "unknown")
387
388/*
389 * Print log messages with the context process name/pid:
390 * [...] kgsl kgsl-3d0: kgsl-api-test[22182]:
391 */
392
393#define pr_context(_d, _c, fmt, args...) \
394 dev_err((_d)->dev, "%s[%d]: " fmt, \
395 _context_comm((_c)), \
396 (_c)->proc_priv->pid, ##args)
397
398/**
399 * struct kgsl_process_private - Private structure for a KGSL process (across
400 * all devices)
401 * @priv: Internal flags, use KGSL_PROCESS_* values
402 * @pid: ID for the task owner of the process
403 * @comm: task name of the process
404 * @mem_lock: Spinlock to protect the process memory lists
405 * @refcount: kref object for reference counting the process
406 * @idr: Iterator for assigning IDs to memory allocations
407 * @pagetable: Pointer to the pagetable owned by this process
408 * @kobj: Pointer to a kobj for the sysfs directory for this process
409 * @debug_root: Pointer to the debugfs root for this process
410 * @stats: Memory allocation statistics for this process
411 * @syncsource_idr: sync sources created by this process
412 * @syncsource_lock: Spinlock to protect the syncsource idr
413 * @fd_count: Counter for the number of FDs for this process
414 */
415struct kgsl_process_private {
416 unsigned long priv;
417 pid_t pid;
418 char comm[TASK_COMM_LEN];
419 spinlock_t mem_lock;
420 struct kref refcount;
421 struct idr mem_idr;
422 struct kgsl_pagetable *pagetable;
423 struct list_head list;
424 struct kobject kobj;
425 struct dentry *debug_root;
426 struct {
427 uint64_t cur;
428 uint64_t max;
429 } stats[KGSL_MEM_ENTRY_MAX];
430 struct idr syncsource_idr;
431 spinlock_t syncsource_lock;
432 int fd_count;
433};
434
435/**
436 * enum kgsl_process_priv_flags - Private flags for kgsl_process_private
437 * @KGSL_PROCESS_INIT: Set if the process structure has been set up
438 */
439enum kgsl_process_priv_flags {
440 KGSL_PROCESS_INIT = 0,
441};
442
443struct kgsl_device_private {
444 struct kgsl_device *device;
445 struct kgsl_process_private *process_priv;
446};
447
448/**
449 * struct kgsl_snapshot - details for a specific snapshot instance
450 * @ib1base: Active IB1 base address at the time of fault
451 * @ib2base: Active IB2 base address at the time of fault
452 * @ib1size: Number of DWORDS pending in IB1 at the time of fault
453 * @ib2size: Number of DWORDS pending in IB2 at the time of fault
454 * @ib1dumped: Active IB1 dump status to sansphot binary
455 * @ib2dumped: Active IB2 dump status to sansphot binary
456 * @start: Pointer to the start of the static snapshot region
457 * @size: Size of the current snapshot instance
458 * @ptr: Pointer to the next block of memory to write to during snapshotting
459 * @remain: Bytes left in the snapshot region
460 * @timestamp: Timestamp of the snapshot instance (in seconds since boot)
461 * @mempool: Pointer to the memory pool for storing memory objects
462 * @mempool_size: Size of the memory pool
463 * @obj_list: List of frozen GPU buffers that are waiting to be dumped.
464 * @cp_list: List of IB's to be dumped.
465 * @work: worker to dump the frozen memory
466 * @dump_gate: completion gate signaled by worker when it is finished.
467 * @process: the process that caused the hang, if known.
468 * @sysfs_read: An atomic for concurrent snapshot reads via syfs.
469 */
470struct kgsl_snapshot {
471 uint64_t ib1base;
472 uint64_t ib2base;
473 unsigned int ib1size;
474 unsigned int ib2size;
475 bool ib1dumped;
476 bool ib2dumped;
477 u8 *start;
478 size_t size;
479 u8 *ptr;
480 size_t remain;
481 unsigned long timestamp;
482 u8 *mempool;
483 size_t mempool_size;
484 struct list_head obj_list;
485 struct list_head cp_list;
486 struct work_struct work;
487 struct completion dump_gate;
488 struct kgsl_process_private *process;
489 atomic_t sysfs_read;
490};
491
492/**
493 * struct kgsl_snapshot_object - GPU memory in the snapshot
494 * @gpuaddr: The GPU address identified during snapshot
495 * @size: The buffer size identified during snapshot
496 * @offset: offset from start of the allocated kgsl_mem_entry
497 * @type: SNAPSHOT_OBJ_TYPE_* identifier.
498 * @entry: the reference counted memory entry for this buffer
499 * @node: node for kgsl_snapshot.obj_list
500 */
501struct kgsl_snapshot_object {
502 uint64_t gpuaddr;
503 uint64_t size;
504 uint64_t offset;
505 int type;
506 struct kgsl_mem_entry *entry;
507 struct list_head node;
508};
509
510struct kgsl_device *kgsl_get_device(int dev_idx);
511
512static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
513 unsigned int type, uint64_t size)
514{
515 priv->stats[type].cur += size;
516 if (priv->stats[type].max < priv->stats[type].cur)
517 priv->stats[type].max = priv->stats[type].cur;
518}
519
520static inline void kgsl_regread(struct kgsl_device *device,
521 unsigned int offsetwords,
522 unsigned int *value)
523{
524 device->ftbl->regread(device, offsetwords, value);
525}
526
527static inline void kgsl_regwrite(struct kgsl_device *device,
528 unsigned int offsetwords,
529 unsigned int value)
530{
531 device->ftbl->regwrite(device, offsetwords, value);
532}
533
534static inline void kgsl_regrmw(struct kgsl_device *device,
535 unsigned int offsetwords,
536 unsigned int mask, unsigned int bits)
537{
538 unsigned int val = 0;
539
540 device->ftbl->regread(device, offsetwords, &val);
541 val &= ~mask;
542 device->ftbl->regwrite(device, offsetwords, val | bits);
543}
544
545static inline int kgsl_idle(struct kgsl_device *device)
546{
547 return device->ftbl->idle(device);
548}
549
550static inline unsigned int kgsl_gpuid(struct kgsl_device *device,
551 unsigned int *chipid)
552{
553 return device->ftbl->gpuid(device, chipid);
554}
555
556static inline int kgsl_create_device_sysfs_files(struct device *root,
557 const struct device_attribute **list)
558{
559 int ret = 0, i;
560
561 for (i = 0; list[i] != NULL; i++)
562 ret |= device_create_file(root, list[i]);
563 return ret;
564}
565
566static inline void kgsl_remove_device_sysfs_files(struct device *root,
567 const struct device_attribute **list)
568{
569 int i;
570
571 for (i = 0; list[i] != NULL; i++)
572 device_remove_file(root, list[i]);
573}
574
575static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
576{
577 int i;
578
579 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
580 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
581 return kgsl_driver.devp[i];
582 }
583
584 return NULL;
585}
586
587static inline int kgsl_state_is_awake(struct kgsl_device *device)
588{
589 if (device->state == KGSL_STATE_ACTIVE ||
590 device->state == KGSL_STATE_AWARE)
591 return true;
592 else
593 return false;
594}
595
596int kgsl_readtimestamp(struct kgsl_device *device, void *priv,
597 enum kgsl_timestamp_type type, unsigned int *timestamp);
598
599int kgsl_check_timestamp(struct kgsl_device *device,
600 struct kgsl_context *context, unsigned int timestamp);
601
602int kgsl_device_platform_probe(struct kgsl_device *device);
603
604void kgsl_device_platform_remove(struct kgsl_device *device);
605
606const char *kgsl_pwrstate_to_str(unsigned int state);
607
608int kgsl_device_snapshot_init(struct kgsl_device *device);
609void kgsl_device_snapshot(struct kgsl_device *device,
610 struct kgsl_context *context);
611void kgsl_device_snapshot_close(struct kgsl_device *device);
612void kgsl_snapshot_save_frozen_objs(struct work_struct *work);
613
614void kgsl_events_init(void);
615void kgsl_events_exit(void);
616
617void kgsl_del_event_group(struct kgsl_event_group *group);
618
619void kgsl_add_event_group(struct kgsl_event_group *group,
620 struct kgsl_context *context, const char *name,
621 readtimestamp_func readtimestamp, void *priv);
622
623void kgsl_cancel_events_timestamp(struct kgsl_device *device,
624 struct kgsl_event_group *group, unsigned int timestamp);
625void kgsl_cancel_events(struct kgsl_device *device,
626 struct kgsl_event_group *group);
627void kgsl_cancel_event(struct kgsl_device *device,
628 struct kgsl_event_group *group, unsigned int timestamp,
629 kgsl_event_func func, void *priv);
630bool kgsl_event_pending(struct kgsl_device *device,
631 struct kgsl_event_group *group, unsigned int timestamp,
632 kgsl_event_func func, void *priv);
633int kgsl_add_event(struct kgsl_device *device, struct kgsl_event_group *group,
634 unsigned int timestamp, kgsl_event_func func, void *priv);
635void kgsl_process_event_group(struct kgsl_device *device,
636 struct kgsl_event_group *group);
637void kgsl_flush_event_group(struct kgsl_device *device,
638 struct kgsl_event_group *group);
639void kgsl_process_event_groups(struct kgsl_device *device);
640
641void kgsl_context_destroy(struct kref *kref);
642
643int kgsl_context_init(struct kgsl_device_private *dev_priv,
644 struct kgsl_context *context);
645
646void kgsl_context_dump(struct kgsl_context *context);
647
648int kgsl_memfree_find_entry(pid_t ptname, uint64_t *gpuaddr,
649 uint64_t *size, uint64_t *flags, pid_t *pid);
650
651long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
652
653long kgsl_ioctl_copy_in(unsigned int kernel_cmd, unsigned int user_cmd,
654 unsigned long arg, unsigned char *ptr);
655
656long kgsl_ioctl_copy_out(unsigned int kernel_cmd, unsigned int user_cmd,
657 unsigned long arg, unsigned char *ptr);
658
Tarun Karra2b8b3632016-11-14 16:38:27 -0800659void kgsl_sparse_bind(struct kgsl_process_private *private,
660 struct kgsl_drawobj_sparse *sparse);
661
Shrenuj Bansala419c792016-10-20 14:05:11 -0700662/**
663 * kgsl_context_put() - Release context reference count
664 * @context: Pointer to the KGSL context to be released
665 *
666 * Reduce the reference count on a KGSL context and destroy it if it is no
667 * longer needed
668 */
669static inline void
670kgsl_context_put(struct kgsl_context *context)
671{
672 if (context)
673 kref_put(&context->refcount, kgsl_context_destroy);
674}
675
676/**
677 * kgsl_context_detached() - check if a context is detached
678 * @context: the context
679 *
680 * Check if a context has been destroyed by userspace and is only waiting
681 * for reference counts to go away. This check is used to weed out
682 * contexts that shouldn't use the gpu so NULL is considered detached.
683 */
684static inline bool kgsl_context_detached(struct kgsl_context *context)
685{
686 return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_DETACHED,
687 &context->priv));
688}
689
690/**
691 * kgsl_context_invalid() - check if a context is invalid
692 * @context: the context
693 *
694 * Check if a context has been invalidated by the kernel and may no
695 * longer use the GPU.
696 */
697static inline bool kgsl_context_invalid(struct kgsl_context *context)
698{
699 return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_INVALID,
700 &context->priv));
701}
702
703
704/**
705 * kgsl_context_get() - get a pointer to a KGSL context
706 * @device: Pointer to the KGSL device that owns the context
707 * @id: Context ID
708 *
709 * Find the context associated with the given ID number, increase the reference
710 * count on it and return it. The caller must make sure that this call is
711 * paired with a kgsl_context_put. This function is for internal use because it
712 * doesn't validate the ownership of the context with the calling process - use
713 * kgsl_context_get_owner for that
714 */
715static inline struct kgsl_context *kgsl_context_get(struct kgsl_device *device,
716 uint32_t id)
717{
718 int result = 0;
719 struct kgsl_context *context = NULL;
720
721 read_lock(&device->context_lock);
722
723 context = idr_find(&device->context_idr, id);
724
725 /* Don't return a context that has been detached */
726 if (kgsl_context_detached(context))
727 context = NULL;
728 else
729 result = kref_get_unless_zero(&context->refcount);
730
731 read_unlock(&device->context_lock);
732
733 if (!result)
734 return NULL;
735 return context;
736}
737
738/**
739 * _kgsl_context_get() - lightweight function to just increment the ref count
740 * @context: Pointer to the KGSL context
741 *
742 * Get a reference to the specified KGSL context structure. This is a
743 * lightweight way to just increase the refcount on a known context rather than
744 * walking through kgsl_context_get and searching the iterator
745 */
746static inline int _kgsl_context_get(struct kgsl_context *context)
747{
748 int ret = 0;
749
750 if (context)
751 ret = kref_get_unless_zero(&context->refcount);
752
753 return ret;
754}
755
756/**
757 * kgsl_context_get_owner() - get a pointer to a KGSL context in a specific
758 * process
759 * @dev_priv: Pointer to the process struct
760 * @id: Context ID to return
761 *
762 * Find the context associated with the given ID number, increase the reference
763 * count on it and return it. The caller must make sure that this call is
764 * paired with a kgsl_context_put. This function validates that the context id
765 * given is owned by the dev_priv instancet that is passed in. See
766 * kgsl_context_get for the internal version that doesn't do the check
767 */
768static inline struct kgsl_context *kgsl_context_get_owner(
769 struct kgsl_device_private *dev_priv, uint32_t id)
770{
771 struct kgsl_context *context;
772
773 context = kgsl_context_get(dev_priv->device, id);
774
775 /* Verify that the context belongs to current calling fd. */
776 if (context != NULL && context->dev_priv != dev_priv) {
777 kgsl_context_put(context);
778 return NULL;
779 }
780
781 return context;
782}
783
784/**
785 * kgsl_process_private_get() - increment the refcount on a
786 * kgsl_process_private struct
787 * @process: Pointer to the KGSL process_private
788 *
789 * Returns 0 if the structure is invalid and a reference count could not be
790 * obtained, nonzero otherwise.
791 */
792static inline int kgsl_process_private_get(struct kgsl_process_private *process)
793{
794 int ret = 0;
795
796 if (process != NULL)
797 ret = kref_get_unless_zero(&process->refcount);
798 return ret;
799}
800
801void kgsl_process_private_put(struct kgsl_process_private *private);
802
803
804struct kgsl_process_private *kgsl_process_private_find(pid_t pid);
805
806/**
807 * kgsl_property_read_u32() - Read a u32 property from the device tree
808 * @device: Pointer to the KGSL device
809 * @prop: String name of the property to query
810 * @ptr: Pointer to the variable to store the property
811 */
812static inline int kgsl_property_read_u32(struct kgsl_device *device,
813 const char *prop, unsigned int *ptr)
814{
815 return of_property_read_u32(device->pdev->dev.of_node, prop, ptr);
816}
817
818/**
819 * kgsl_sysfs_store() - parse a string from a sysfs store function
820 * @buf: Incoming string to parse
821 * @ptr: Pointer to an unsigned int to store the value
822 */
823static inline int kgsl_sysfs_store(const char *buf, unsigned int *ptr)
824{
825 unsigned int val;
826 int rc;
827
828 rc = kstrtou32(buf, 0, &val);
829 if (rc)
830 return rc;
831
832 if (ptr)
833 *ptr = val;
834
835 return 0;
836}
837
838/*
839 * A helper macro to print out "not enough memory functions" - this
840 * makes it easy to standardize the messages as well as cut down on
841 * the number of strings in the binary
842 */
843#define SNAPSHOT_ERR_NOMEM(_d, _s) \
844 KGSL_DRV_ERR((_d), \
845 "snapshot: not enough snapshot memory for section %s\n", (_s))
846
847/**
848 * struct kgsl_snapshot_registers - list of registers to snapshot
849 * @regs: Pointer to an array of register ranges
850 * @count: Number of entries in the array
851 */
852struct kgsl_snapshot_registers {
853 const unsigned int *regs;
854 unsigned int count;
855};
856
857size_t kgsl_snapshot_dump_registers(struct kgsl_device *device, u8 *buf,
858 size_t remain, void *priv);
859
860void kgsl_snapshot_indexed_registers(struct kgsl_device *device,
861 struct kgsl_snapshot *snapshot, unsigned int index,
862 unsigned int data, unsigned int start, unsigned int count);
863
864int kgsl_snapshot_get_object(struct kgsl_snapshot *snapshot,
865 struct kgsl_process_private *process, uint64_t gpuaddr,
866 uint64_t size, unsigned int type);
867
868int kgsl_snapshot_have_object(struct kgsl_snapshot *snapshot,
869 struct kgsl_process_private *process,
870 uint64_t gpuaddr, uint64_t size);
871
872struct adreno_ib_object_list;
873
874int kgsl_snapshot_add_ib_obj_list(struct kgsl_snapshot *snapshot,
875 struct adreno_ib_object_list *ib_obj_list);
876
877void kgsl_snapshot_add_section(struct kgsl_device *device, u16 id,
878 struct kgsl_snapshot *snapshot,
879 size_t (*func)(struct kgsl_device *, u8 *, size_t, void *),
880 void *priv);
881
882/**
883 * struct kgsl_pwr_limit - limit structure for each client
884 * @node: Local list node for the limits list
885 * @level: requested power level
886 * @device: pointer to the device structure
887 */
888struct kgsl_pwr_limit {
889 struct list_head node;
890 unsigned int level;
891 struct kgsl_device *device;
892};
893
894#endif /* __KGSL_DEVICE_H */