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