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